<p>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:</p>

<blockquote>
  <p>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.</p>
</blockquote>

<p>The code I am using to create the instance is:</p>

<pre><code>myServiceClient = new MyServiceClient();
</code></pre>

<p>where MyServiceClient inherits from</p>

<p>System.ServiceModel.ClientBase</p>

<p>How do I solve this?</p>

<p>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.</p>

[{'answer_id': 25004, 'author': 'Richard Morgan', 'author_id': 2258, 'author_profile': 'https://Stackoverflow.com/users/2258', 'pm_score': 0, 'selected': False, 'text': '<p>It would probably help if you posted your app.config file, since this kind of error tends to point to a problem in the <code>&lt;endpoint&gt;</code> block.  Make sure the contract attribute seems right to you.</p>\n\n<p>Edit: Try fully qualifying your contract value; use the full namespace.  I think that is needed.</p>\n'}, {'answer_id': 25075, 'author': 'aku', 'author_id': 1196, 'author_profile': 'https://Stackoverflow.com/users/1196', 'pm_score': 3, 'selected': True, 'text': "<blockquote>\n  <p>Here is my app.config file of the class library:</p>\n</blockquote>\n\n<p>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.</p>\n"}, {'answer_id': 1711550, 'author': 'NealWalters', 'author_id': 160245, 'author_profile': 'https://Stackoverflow.com/users/160245', 'pm_score': 1, 'selected': False, 'text': '<p>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.  </p>\n\n<p>But as Richard said above, I had to fully-qualify the namespace.  It\'s a bit of a pain. \nBelow is exactly what I changed.  The pain is that I had to change it in two places, \none 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. </p>\n\n<p>1)  You must actually change the ConfigurationName in the reference.cs as follows: </p>\n\n<p>From:    <code>[System.ServiceModel.ServiceContractAttribute(Namespace = "http://TFBIC.RCT.BizTalk.Orchestrations", ConfigurationName = " RCTWebService.WcfService_TFBIC_RCT_BizTalk_Orchestrations")]</code></p>\n\n<p>To:    <code>[System.ServiceModel.ServiceContractAttribute(Namespace = "http://TFBIC.RCT.BizTalk.Orchestrations", ConfigurationName = "TFBIC.RCT.HIP.Components.RCTWebService.WcfService_TFBIC_RCT_BizTalk_Orchestrations")]</code></p>\n\n<p>2)  and then also change the “contract” value in all related app.config (for .dll’s and .exe’s) as follows:</p>\n\n<p>From:  </p>\n\n<p>&lt;endpoint address=<a href="http://nxwtest08bt1.dev.txfb-ins.com/TFBIC.RCT.BizTalk.Orchestrations/WcfService_TFBIC_RCT_BizTalk_Orchestrations.svc" rel="nofollow noreferrer">http://nxwtest08bt1.dev.txfb-ins.com/TFBIC.RCT.BizTalk.Orchestrations/WcfService_TFBIC_RCT_BizTalk_Orchestrations.svc</a>\nbinding="wsHttpBinding" bindingConfiguration="WSHttpBinding_ITwoWayAsync"\n                contract="RCTWebService.WcfService_TFBIC_RCT_BizTalk_Orchestrations"\n                name="WSHttpBinding_ITwoWayAsync"&gt;</p>\n\n<p>To:   </p>\n\n<p>&lt;endpoint address=<a href="http://nxwtest08bt1.dev.txfb-ins.com/TFBIC.RCT.BizTalk.Orchestrations/WcfService_TFBIC_RCT_BizTalk_Orchestrations.svc" rel="nofollow noreferrer">http://nxwtest08bt1.dev.txfb-ins.com/TFBIC.RCT.BizTalk.Orchestrations/WcfService_TFBIC_RCT_BizTalk_Orchestrations.svc</a>\nbinding="wsHttpBinding" bindingConfiguration="WSHttpBinding_ITwoWayAsync"\ncontract=" TFBIC.RCT.HIP.Components.RCTWebService.WcfService_TFBIC_RCT_BizTalk_Orchestrations"          name="WSHttpBinding_ITwoWayAsync"&gt;</p>\n\n<p>Just to be clear - how did I know what the full namespace was?\nThe program\'s namespace was TFBIC.RCT.HIP.  Inside that, the C# code has one additional \nnamespace statement:</p>\n\n<pre><code>namespace RCTHipComponents\n</code></pre>\n'}, {'answer_id': 2423898, 'author': 'Trond', 'author_id': 287647, 'author_profile': 'https://Stackoverflow.com/users/287647', 'pm_score': 3, 'selected': False, 'text': '<p>Or you can set the endpoint in your code:</p>\n\n<p><a href="http://msdn.microsoft.com/en-us/library/ms731862.aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/ms731862.aspx</a></p>\n\n<pre><code>BasicHttpBinding binding = new BasicHttpBinding();\nEndpointAddress address = new EndpointAddress("http://url-to-service/");\n\n// Create a client that is configured with this address and binding.\nMyServiceClient client = new MyServiceClient(binding, address);\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/24993', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/31505/']