不多说了,直接上代码吧。。。。
服务端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>
下面才是我想要的东西,弄这个花了我半天时间
客户端代码注册配置文件
public class ClientFactory : IDisposable
{
static EndpointAddress serviceAddress;
static WSHttpBinding binding;
ChannelFactory factory;
TClient proxy;
OperationContextScope scope;
public TClient CreateClient()
{
factory = new ChannelFactory(binding, serviceAddress);
foreach (OperationDescription op in factory.Endpoint.Contract.Operations)
{
DataContractSerializerOperationBehavior dataContractBehavior = op.Behaviors.Find()
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 mhg = new MessageHeader(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