4

I have created a class library that should connect to a WCF endpoint project I am hosting. The client project has commandlets defined that should interact with the service.

However, I keep getting the following error:

     Could not find default endpoint element that references contract Service1.MyService 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.

Do you know what the problem might be?

EDIT What I have is a single Class Library defining cmdlets. I use an .psd1 file to Import-Module which uses the dll files produced.

EDIT2 Once again, I don't have a project referencing my library. It is the powershell that calls the commandlets defined and these cmdlets should connect to the WCF endpoint

Thanks

Saher Ahwal
  • 9,015
  • 32
  • 84
  • 152
  • Old post, But I found an observation to this error too. If you have 2 assemblies, where one assembly calls a method in the other assembly which has the service reference, and the other assembly is the startup assembly, your service configuration needs to be in the startup assembly too. – Eon Nov 30 '15 at 10:39

3 Answers3

10

have got this to work: here is a clean solution:

internal static class ServiceClass
{

    internal static Object GetWCFSvc(string siteUrl)
    {

        Uri serviceUri = new Uri(siteUrl);
        EndpointAddress endpointAddress = new EndpointAddress(serviceUri);

        //Create the binding here
        Binding binding = BindingFactory.CreateInstance();

        ServiceClient client = new ServiceClient(binding, endpointAddress);            
        return client;
    }


}

internal static class BindingFactory
{
    internal static Binding CreateInstance()
    {
        BasicHttpBinding binding = new BasicHttpBinding();
        binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
        binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
        binding.UseDefaultWebProxy = true;
        return binding;
    }

}
Saher Ahwal
  • 9,015
  • 32
  • 84
  • 152
0

create configuration file with endpoints in startup project

burning_LEGION
  • 13,246
  • 8
  • 40
  • 52
  • what is a startup project. All I have is one project that defines cmdlets and is a class library. I use a psd1 file to import-module for cmdlets – Saher Ahwal Jul 19 '12 at 21:56
  • 1
    He means that the project using the library should have a config file containing the endpoint. NOT the library itself. – Silvermind Jul 19 '12 at 21:57
  • I understand but the powershell is what is calling into my library not another project – Saher Ahwal Jul 19 '12 at 22:07
0

When you're running the code in your cmdlet assembly, the executing process is powershell.exe (in %windir%\system32\WindowsPowershell\v1.0 or %windir%\syswow64\WindowsPowershell\v1.0), so any System.ServiceModel configuration will be need to be defined in the powershell config file (powershell.exe.config).

I'm guessing this is probably not a practical option, so you'll probably need to configure your service client or channel factory manually rather than via the application configuration file.

See here for an example: http://msdn.microsoft.com/en-us/library/ms734681.aspx

Another option may be to use an Activation Configuration File: http://msdn.microsoft.com/en-us/library/ff361644.aspx

I'm not sure how well this will work for service configuration elements.

lesscode
  • 6,221
  • 30
  • 58
  • I will look into that. Is that the only way this can be done besides changing the powershell exe config? – Saher Ahwal Jul 20 '12 at 02:56
  • As far as I know there's no way to load configuration sections from anywhere other than the application configuration file, and in your case the application is powershell. – lesscode Jul 20 '12 at 03:04