[WCF] Could not find default endpoint element that references contract 'xx' in the ServiceModel
来源:互联网 发布:mysql密码破解工具 编辑:程序博客网 时间:2024/05/02 14:56
Background:
I have a WCF service which is being hosted in IIS. I have a WCF a common library to invoke this service. I have used svcutil to build the proxy class and configuration file and then added those to my client project. Also i have a client to invoke common library. It will throw following exception.
Could not find default endpoint element that references contract '**.IService' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element.
Solution:
Add config file of IIS into common library and client program. That is, Service, Library and Client should have same app config for WCF service.
Note:
If your client app is calledclient.exe
, is your config in the same directory as the EXE and called Client.exe.config
If you client app is a vs addin, It's config called devenv.exe.config under folder IDE of Visual Studio.That is, it attempts to load devenv.exe.config instead of client.exe.config file. There are 2 methods to resolve this issue.
1. Set app config location from current domain.
AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", @"C:\abc\client.exe.config"); ResetConfigMechanism();
private static void ResetConfigMechanism() { typeof(ConfigurationManager) .GetField("s_initState", BindingFlags.NonPublic | BindingFlags.Static) .SetValue(null, 0); typeof(ConfigurationManager) .GetField("s_configSystem", BindingFlags.NonPublic | BindingFlags.Static) .SetValue(null, null); typeof(ConfigurationManager) .Assembly.GetTypes() .Where(x => x.FullName == "System.Configuration.ClientConfigPaths") .First() .GetField("s_current", BindingFlags.NonPublic | BindingFlags.Static) .SetValue(null, null); }
2. Set config content from code not app.config.
public class ValidatorClass { static WSHttpBinding BindingConfigWSHttpBinding; static BasicHttpBinding BindingConfigBasicHttpBinding; static EndpointIdentity DNSIdentity; static Uri URI; static ContractDescription ConfDescription; static ValidatorClass() { // In constructor initializing configuration elements by code string bingdingName = "BasicHttpBinding_IWFInfo"; string address = "http://abcserver/wftest/WFInfo.svc"; BindingConfigWSHttpBinding = ConfigBindingWSHttpBinding(bingdingName); BindingConfigBasicHttpBinding = ConfigBindingBasicHttpBinding(bingdingName); DNSIdentity = ConfigEndPoint(); URI = ConfigURI(address); ConfDescription = ConfigContractDescription(); } public static WFInfoClient CreateInstanceWFInfoClient() { var Address = new EndpointAddress(URI, DNSIdentity); var Client = new WFInfoClient(BindingConfigBasicHttpBinding, Address); Client.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.PeerTrust; Client.Endpoint.Contract = ConfDescription; Client.ClientCredentials.UserName.UserName = Environment.UserName; // Client.ClientCredentials.UserName.Password = "companyPassword"; Password is not necessary // Client.Open(); //var CatchData = Client.GetTestWFAllParser(); Test return Client; } public static BasicHttpBinding ConfigBindingBasicHttpBinding(string bindingName) { BasicHttpBinding binding = new BasicHttpBinding(); // I think most (or all) of these are defaults--I just copied them from app.config: binding.Name = bindingName; binding.SendTimeout = TimeSpan.FromMinutes(1); binding.OpenTimeout = TimeSpan.FromMinutes(5); binding.CloseTimeout = TimeSpan.FromMinutes(5); binding.ReceiveTimeout = TimeSpan.FromMinutes(10); binding.AllowCookies = false; binding.BypassProxyOnLocal = false; binding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard; binding.MessageEncoding = WSMessageEncoding.Text; binding.TextEncoding = System.Text.Encoding.UTF8; binding.TransferMode = TransferMode.Buffered; binding.UseDefaultWebProxy = true; binding.MaxBufferSize = 32473648; binding.MaxBufferPoolSize = 32473648; binding.MaxReceivedMessageSize = 32473648; return binding; } public static WSHttpBinding ConfigBindingWSHttpBinding(string bindingName) { // ----- Programmatic definition of the SomeService Binding ----- var wsHttpBinding = new WSHttpBinding(); wsHttpBinding.Name = bindingName; wsHttpBinding.CloseTimeout = TimeSpan.FromMinutes(1); wsHttpBinding.OpenTimeout = TimeSpan.FromMinutes(1); wsHttpBinding.ReceiveTimeout = TimeSpan.FromMinutes(10); wsHttpBinding.SendTimeout = TimeSpan.FromMinutes(1); wsHttpBinding.BypassProxyOnLocal = false; wsHttpBinding.TransactionFlow = false; wsHttpBinding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard; wsHttpBinding.MaxBufferPoolSize = 524288; wsHttpBinding.MaxReceivedMessageSize = 65536; wsHttpBinding.MessageEncoding = WSMessageEncoding.Text; wsHttpBinding.TextEncoding = Encoding.UTF8; wsHttpBinding.UseDefaultWebProxy = true; wsHttpBinding.AllowCookies = false; wsHttpBinding.ReaderQuotas.MaxDepth = 32; wsHttpBinding.ReaderQuotas.MaxArrayLength = 16384; wsHttpBinding.ReaderQuotas.MaxStringContentLength = 8192; wsHttpBinding.ReaderQuotas.MaxBytesPerRead = 4096; wsHttpBinding.ReaderQuotas.MaxNameTableCharCount = 16384; wsHttpBinding.ReliableSession.Ordered = true; wsHttpBinding.ReliableSession.InactivityTimeout = TimeSpan.FromMinutes(10); wsHttpBinding.ReliableSession.Enabled = false; wsHttpBinding.Security.Mode = SecurityMode.Message; wsHttpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate; wsHttpBinding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None; wsHttpBinding.Security.Transport.Realm = ""; wsHttpBinding.Security.Message.NegotiateServiceCredential = true; wsHttpBinding.Security.Message.ClientCredentialType = MessageCredentialType.UserName; wsHttpBinding.Security.Message.AlgorithmSuite = System.ServiceModel.Security.SecurityAlgorithmSuite.Basic256; // ----------- End Programmatic definition of the SomeServiceServiceBinding -------------- return wsHttpBinding; } public static Uri ConfigURI(string address) { // ----- Programmatic definition of the Service URI configuration ----- Uri URI = new Uri(address); return URI; } public static EndpointIdentity ConfigEndPoint() { // ----- Programmatic definition of the Service EndPointIdentitiy configuration ----- EndpointIdentity DNSIdentity = EndpointIdentity.CreateDnsIdentity("tempCert"); return DNSIdentity; } public static ContractDescription ConfigContractDescription() { // ----- Programmatic definition of the Service ContractDescription Binding ----- ContractDescription Contract = ContractDescription.GetContract(typeof(ProtocolService.IWFInfo), typeof(WFInfoClient)); return Contract; } }
You can't just take the output.config
from svcutil
- you need to either add a app.config
to your client console project (which will be renamed to client.exe.config when compiling), or you need to copy/rename the output.config
to client.exe.config
in order for your client app to find and use it.
1. http://stackoverflow.com/questions/13543304/how-to-load-wcf-service-bindings-for-a-visual-studio-2010-addin
2. http://stackoverflow.com/questions/2192970/wcf-could-not-find-default-endpoint-element-that-references-contract-iservice
- [WCF] Could not find default endpoint element that references contract 'xx' in the ServiceModel
- Could not find default endpoint element that references contract 'wcfXXXXXXXXXXX' in the ServiceMode
- Could not find default endpoint element that references contract 'wcfXXXXXXXXXXX' in the ServiceMode
- 解决 关于Silverlight使用WCF时抛出异常 Could not find default endpoint element that references contract...
- "Could not find default endpoint element that references contract "的解决
- Struts2的Invalid action class configuration that references an unknown class或者Could not find action
- The AssetBundle could not be loaded because it references scripts that are not compatible ...
- Could not find schema information for the element 'supportedRuntime'.
- WCF分布式开发常见错误(5)Could not find a base address that matches scheme
- Default WCF bindings for endpoint
- Find the element that appears once
- Find the element that appears once
- Unable to instantiate default tuplizer/Could not find a getter for orders in class
- could not find the adb
- Swift: could not find an overload for '+' that accepts the supplied arguments
- Python安装PIL遇到‘Could not find a version that satisfies the requirement PIL’
- python环境选择和安装,解决could not find a version that satisfies the requirement tensorflow
- mac 安装 tensorflow 提示 Could not find a version that satisfies the requirement
- 排序算法
- CentOS相关问题
- 用OpenSceneGraph实现的NeHe OpenGL教程 - 第三十三课
- vc2008配置opengl开发环境
- 关于程序中日志打印和异常抛出
- [WCF] Could not find default endpoint element that references contract 'xx' in the ServiceModel
- NFC高级
- Magento 架构(Architecture)
- JPA 注解
- 专访顾伟:从机械工程师到资深软件工程师、再到微软MVP的蜕变
- 自定义快捷模板
- 01_translation_avtivity生命周期01
- TCP的那些事儿(上)
- 如何生成邀请码?