views:

2372

answers:

5

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.

A: 

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.

Richard Morgan
A: 

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

<system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="binding_IMyService" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                    useDefaultWebProxy="true">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <security mode="None">
                        <transport clientCredentialType="None" proxyCredentialType="None"
                            realm="" />
                        <message clientCredentialType="UserName" algorithmSuite="Default" />
                    </security>
                </binding>
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:3936/MyService.svc" binding="basicHttpBinding"
                bindingConfiguration="binding_IMyService" contract="MyServiceReference.IMyService"
                name="binding_IMyService" />
        </client>
    </system.serviceModel>
Serhat Özgel
+2  A: 

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.

aku
A: 

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%5FTFBIC%5FRCT%5FBizTalk%5FOrchestrations.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%5FTFBIC%5FRCT%5FBizTalk%5FOrchestrations.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
NealWalters
A: 

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);
Trond