WCF客户端C#代码 配置config文件

不多说了,直接上代码吧。。。。

服务端Web.config文件中bindings配置

复制代码
   <bindings>
      <wsHttpBinding>
        <binding name="httpconf" closeTimeout="10:10:10"
          openTimeout="10:10:10" receiveTimeout="10:10:10" sendTimeout="10:10:10"
          allowCookies="true" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
           maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" >
          <reliableSession ordered="true" inactivityTimeout="00:20:00"
            enabled="false" />
          <security mode="Message">
            <transport clientCredentialType="Windows" proxyCredentialType="None"
             realm="" />
            <message clientCredentialType="Windows" negotiateServiceCredential="true"
              algorithmSuite="Default" />
          </security>
        </binding>
      </wsHttpBinding>
 </bindings>
复制代码

服务端Web.config文件中behavior配置

<behavior name="Card_WcfService.WCF.CardInfoServiceBehavior">
          <dataContractSerializer maxItemsInObjectGraph="2147483647" />
          <serviceMetadata httpGetEnabled="true" />
          <serviceTimeouts transactionTimeout="00:10:00" />
          <serviceDebug includeExceptionDetailInFaults="false" />
          <serviceThrottling maxConcurrentCalls="2147483647" maxConcurrentInstances="2147483647" maxConcurrentSessions="2147483647" />
 </behavior>

服务端Web.config文件中service配置

 <service behaviorConfiguration="Card_WcfService.WCF.CardInfoServiceBehavior"
        name="Card_WcfService.WCF.CardInfoService">
        <endpoint address="" binding="wsHttpBinding" contract="Card_SystemAPI.ICardInfoService"  bindingConfiguration="httpconf">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>      
      </service>

下面才是我想要的东西,弄这个花了我半天时间
客户端代码注册配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
public class ClientFactory<TClient> : IDisposable
   {
       static EndpointAddress serviceAddress;
       static WSHttpBinding binding;
       ChannelFactory<TClient> factory;
       TClient proxy;
       OperationContextScope scope;
       public TClient CreateClient()
       {
           factory = new ChannelFactory<TClient>(binding, serviceAddress);
           foreach (OperationDescription op in factory.Endpoint.Contract.Operations)
           {
 
               DataContractSerializerOperationBehavior dataContractBehavior = op.Behaviors.Find<DataContractSerializerOperationBehavior>()
                   as DataContractSerializerOperationBehavior;
 
               if (dataContractBehavior != null)
               {
 
                   dataContractBehavior.MaxItemsInObjectGraph = 2147483647;
 
               }
 
           }
           proxy = factory.CreateChannel();
           ((IClientChannel)proxy).Faulted += new EventHandler(a_Faulted);
           scope = new OperationContextScope(((IClientChannel)proxy));
           var curId = CacheStrategy.CurUser == null ?Guid.Empty : CacheStrategy.CurUser.AutoID;
           MessageHeader<Guid> mhg = new MessageHeader<Guid>(curId);
           MessageHeader untyped = mhg.GetUntypedHeader("token", "ns");
           OperationContext.Current.OutgoingMessageHeaders.Add(untyped);
           return proxy;
       }
       void a_Faulted(object sender, EventArgs e)
       {
           //todo:此处得不到异常的内容
       }
       public void Dispose()
       {
           try
           {
               scope.Dispose();
               ((IClientChannel)proxy).Close();
               factory.Close();
           }
           catch
           {
           }
       }
       static ClientFactory()
       {
           var surl = ConfigurationManager.AppSettings["ServiceURL"];
           var iname = typeof(TClient).FullName.Substring("Card_SystemAPI.I".Length);
           var sname = string.Format("{0}.svc", iname);
           var url = Path.Combine(surl, sname);
           serviceAddress = new EndpointAddress(url);
           binding = new WSHttpBinding();
           binding.CloseTimeout = new TimeSpan(10, 10, 10);
           binding.OpenTimeout = new TimeSpan(10, 10, 10);
           binding.SendTimeout = new TimeSpan(10, 10, 10);
           binding.ReceiveTimeout = new TimeSpan(10, 10, 10);
           binding.MaxReceivedMessageSize = 2147483647;
           binding.MaxBufferPoolSize = 2147483647;
           binding.AllowCookies = true;
       }
   }

 

QQ群号: 242251580  身份认证:.NET技术讨论

如有转载,请保留原有地址:http://www.cnblogs.com/hank-hu/p/4663568.html                       

posted @   Rach  阅读(4015)  评论(1编辑  收藏  举报
点击右上角即可分享
微信分享提示