views:

160

answers:

2

I have a service I am trying to consume in a unit test. At this point I'm just trying to instantiate the thing. After suffering the "Could not find default endpoint element that references contract" error for hours and unable to figure it out, I completely deleted out the consumer and started from scratch. All I did was add a service reference to my test project, point it at my service, hit "GO" and that's it. Still doesn't work. I didn't touch a line of code, yet it doesn't work right after I let VS build the thing.

Here is the relevant line in my app.config for the test project:

<client>
    <endpoint address="http://mike-laptop/kbs/FFEDI/Service.svc"
       binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IEdiService"
       contract="ServiceReference2.IEdiService" name="BasicHttpBinding_IEdiService" />
</client>

In my unit test, here is my method:

public void CreateWebServiceInstance()
{
    ServiceReference2.EdiServiceClient webService = new ServiceReference2.EdiServiceClient();
    string svcAddress = webService.Endpoint.Address.ToString();
    Console.WriteLine("Address is: " + svcAddress);
    Assert.IsTrue(svcAddress.Equals("http://mike-laptop/kbs/FFEDI/Service.svc"));  // test
}

The error I get is:

System.InvalidOperationException: Could not find default endpoint element that references contract 'ServiceReference2.IEdiService' 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.

Again, I didn't change anything this time. Any ideas?

A: 

Is the service hosted in IIS? Can you hit it in a browser? Do you have a <binding name="BasicHttpBinding_IEdiService">... in your config? Did you try passing the binding config name into the constructor?

iszzypop
Yes, the service is hosted in IIS, and it actually runs for me when I call it outside of the test project (I have a Windows service that calls it via http as a different part of the solution). Here is another line out of my appconfig: "<basicHttpBinding> <binding name="BasicHttpBinding_IEdiService"...", which also looks ok to me. Like I said, the IDE did all this for me. I tried passing the name, too, and I get the same error but it also refers to the binding name (i.e. "Could not find default endpoint element with name="blah blah" that references contract "blah blah")
Mike at KBS
if you don't really need config you could just do it in code.webService = new ServiceReference2.EdiServiceClient(new BasicHttpBinding(), new EndpointAddress("http://mike-laptop/kbs/FFEDI/Service.svc"));
iszzypop
That is helpful; I actually want to know how to do that just for troubleshooting purposes. I hate all this black-box stuff, too hard to track what's actually happening.
Mike at KBS
A: 

Seems fine at first sight to me... some ideas to check / ponder / verify:

  • does your test project's app.config get read at all? E.g. is it being interpreted at all? Is there a TestProject.exe.config in your bin\debug directory? I'm thinking maybe the test runner might be playing some tricks and not reading the config at all.

  • or what happens if you specify the name of the client endpoint when creating your service client?

    ServiceReference2.EdiServiceClient webService = 
        new ServiceReference2.EdiServiceClient("BasicHttpBinding_IEdiService");
    

Does that change anything at all?

marc_s
Specifying the endpoint name doesn't seem to change anything other than a little lengthier error message. I had the same thought about the test runner not reading the app.config. When I do a build, I am not seeing a new config file of any kind in the bin\debug directory. That is curious; I'll have to look into that.
Mike at KBS