[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.

Refefrence:

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

10 0
原创粉丝点击