Question

I have a WCF Web Service which is referenced from a class library. After the project is run, when creating the service client object from inside a class library, I receive an InvalidOperationException with message:

Could not find default endpoint element that references contract 'MyServiceReference.IMyService' 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.

The code I am using to create the instance is:

myServiceClient = new MyServiceClient();

where MyServiceClient inherits from

System.ServiceModel.ClientBase

How do I solve this?

Note: I have a seperate console application which simply creates the same service object and makes calls to it and it works without no problems.

Was it helpful?

Solution

Here is my app.config file of the class library:

You should put this configuration settings to main app's config file. .NET application (which is calling your class library) uses data from it's own config file not from your library config file.

OTHER TIPS

Or you can set the endpoint in your code:

http://msdn.microsoft.com/en-us/library/ms731862.aspx

BasicHttpBinding binding = new BasicHttpBinding();
EndpointAddress address = new EndpointAddress("http://url-to-service/");

// Create a client that is configured with this address and binding.
MyServiceClient client = new MyServiceClient(binding, address);

I had a similar case. I had a class-library that called a web service, then I had an .EXE that called the class-lib's .DLL. I think it's the .EXE's config file that is used and not that of the .DLL config.

But as Richard said above, I had to fully-qualify the namespace. It's a bit of a pain. Below is exactly what I changed. The pain is that I had to change it in two places, one in the reference.cs that is generated when you create a service reference, and the other in the config file. Thus, everytime I change the web service and do an "Update Reference" I have to make the change to the C# code again.

1) You must actually change the ConfigurationName in the reference.cs as follows:

From: [System.ServiceModel.ServiceContractAttribute(Namespace = "http://TFBIC.RCT.BizTalk.Orchestrations", ConfigurationName = " RCTWebService.WcfService_TFBIC_RCT_BizTalk_Orchestrations")]

To: [System.ServiceModel.ServiceContractAttribute(Namespace = "http://TFBIC.RCT.BizTalk.Orchestrations", ConfigurationName = "TFBIC.RCT.HIP.Components.RCTWebService.WcfService_TFBIC_RCT_BizTalk_Orchestrations")]

2) and then also change the “contract” value in all related app.config (for .dll’s and .exe’s) as follows:

From:

<endpoint address=http://nxwtest08bt1.dev.txfb-ins.com/TFBIC.RCT.BizTalk.Orchestrations/WcfService_TFBIC_RCT_BizTalk_Orchestrations.svc binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_ITwoWayAsync" contract="RCTWebService.WcfService_TFBIC_RCT_BizTalk_Orchestrations" name="WSHttpBinding_ITwoWayAsync">

To:

<endpoint address=http://nxwtest08bt1.dev.txfb-ins.com/TFBIC.RCT.BizTalk.Orchestrations/WcfService_TFBIC_RCT_BizTalk_Orchestrations.svc binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_ITwoWayAsync" contract=" TFBIC.RCT.HIP.Components.RCTWebService.WcfService_TFBIC_RCT_BizTalk_Orchestrations" name="WSHttpBinding_ITwoWayAsync">

Just to be clear - how did I know what the full namespace was? The program's namespace was TFBIC.RCT.HIP. Inside that, the C# code has one additional namespace statement:

namespace RCTHipComponents

It would probably help if you posted your app.config file, since this kind of error tends to point to a problem in the <endpoint> block. Make sure the contract attribute seems right to you.

Edit: Try fully qualifying your contract value; use the full namespace. I think that is needed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top