类库项目引用WCF
1.当项目中出现UI-->BLL-->WCF结构时,我们需要调用引用了WCF项目的类库中的服务方法,如果提示找不到相应的服务方法,没有匹配的终结点
(这是因为启动项目时,运行的是UI的Web.Config文件,而WCF服务的客户端配置确在类库配置文件App.Config中,根本就没有读取到)
所以:
一、如果直接服务引用WCF项目,不能正常使用,查看配置文件是否正确,客户端和服务端是否一致
二、如能正常使用,一般的解决方案是:
① 将客户端生成的配置文件中的(app.config文件)<system.serviceModel>节点和节点内所有的内容Copy到UI的Web.config中,放到<configuration>节点下(注意是一级,和<system.web>同级),
显然,如果有多个服务时,这种方法显得很笨拙;
② 还有一种比较方便的方法就是,直接在服务引用了WCF项目的类库中的类里面,用代码配置客户端,具体需要配置哪些项,看你服务端设置了哪些项,自己在做匹配
示例:
1 WSHttpBinding binding = new WSHttpBinding(); 2 binding.TransactionFlow = transactionFlow; 3 binding.Namespace = nameSpace; 4 binding.ReliableSession.Enabled = false; 5 binding.CloseTimeout = TimeSpan.FromMinutes(1); 6 binding.OpenTimeout = TimeSpan.FromMinutes(1); 7 binding.ReceiveTimeout = TimeSpan.FromMinutes(10); 8 binding.SendTimeout = TimeSpan.FromMinutes(60); 9 binding.BypassProxyOnLocal = false; 10 11 binding.MaxBufferPoolSize = 10485760; 12 binding.MaxReceivedMessageSize = 20485760;//262144=256K//2097152=2M 13 binding.MaxReceivedMessageSize = 2048576000;//262144=256K//2097152=2M 14 15 binding.MessageEncoding = WSMessageEncoding.Text; 16 binding.UseDefaultWebProxy = true; 17 binding.AllowCookies = false; 18 binding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard; 19 binding.TextEncoding = Encoding.UTF8; 20 21 //XmlDictionaryReaderQuotas 22 binding.ReaderQuotas.MaxStringContentLength = 2048576000; 23 binding.ReaderQuotas.MaxArrayLength = 20485760; 24 binding.ReaderQuotas.MaxDepth = 32; 25 26 binding.Security.Mode = mode;//SecurityMode 27 28 29 HttpTransportSecurity hts = binding.Security.Transport; 30 hts.ProxyCredentialType = HttpProxyCredentialType.None; 31 hts.ClientCredentialType = HttpClientCredentialType.Windows; 32 hts.Realm = ""; 33 34 35 NonDualMessageSecurityOverHttp mso = binding.Security.Message; 36 mso.ClientCredentialType = MessageCredentialType.Windows; 37 mso.NegotiateServiceCredential = true; 38 mso.EstablishSecurityContext = true; 39 mso.AlgorithmSuite = System.ServiceModel.Security.SecurityAlgorithmSuite.Default;
1 /// <summary> 2 /// 创建终结点 3 /// </summary> 4 /// <param name="uri"></param> 5 /// <returns></returns> 6 private static EndpointAddress CreateEndpointAddress(string uri) 7 { 8 EndpointIdentity ei = EndpointIdentity.CreateDnsIdentity("localhost"); 9 EndpointAddress epa = new EndpointAddress(new Uri(uri), ei, new AddressHeaderCollection()); 10 11 return epa; 12 }
注:本人也是处于WCF学习阶段,这些总结都是在平时自己练习时碰到的,然后自己找的解决方案,可能不是很好,写出来一是为了让自己印象深刻点,二是希望碰到和我一样问题的朋友可以快速的找到可以参考的答案。同时有人要是有好的方案,希望能不吝赐教。谢谢!