1
votes

I've built a small WCF service that I'm trying to build unit tests for. I've deployed this service with a database locally and the service works and can save and read data from the database. I created a separate project within the same solution as a Console Application with a service reference to consume this service.

Here is what the code looks like:

Service Contract

    [ServiceContract]
    public interface IUserService
    {
        //When testing in the browser use verb GET
        [OperationContract]
        [WebInvoke(Method="POST", UriTemplate = "/CreateNewUserAccount?userNameValue={userName})]
        void CreateNewUserAccount(string userName);
    }

Service Implementation:

    public void CreateNewUserAccount(string userName)
    {
        var userContorller = new UserController();
        userContorller.addNewUser(userName);
    }

Console Client with service reference:

        static void Main(string[] args)
        {
            try
            {
                UserServiceClient serviceClient = new UserServiceClient("");
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            Console.ReadKey();
        }

Here is a sample of how I call the service in the browser: http://localhost:8654/Services/UserService/UserService.svc/CreateNewUserAccount?userNameValue=testUser

I've confirmed that this works when I run the service locally. Now the exception I'm receiving from the console client is:

Could not find endpoint element with name '' and contract 'SerficeReference.IUserService' in the ServiceModel client configuration section.

It looks like I'm missing endpoints and nothing was generated in the app.config when I linked the service reference. I've tried numerous configurations in the app.config but I can't seem to get this straight. The exception occurs when I try to instantiate the service client in the console app. My app.config looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
    </startup>
</configuration>

Any help would be appreciated.

2
What you are trying to do (add a service reference to a WCF web HTTP endpoint) does not work! blogs.msdn.com/b/carlosfigueira/archive/2012/03/26/… - tom redfern

2 Answers

0
votes

The reason that no client endpoint configuration is generated in the config file is that generating client code from a RESTful WCF service is not supported.

There is no metadata standard for non-SOAP HTTP services. As a caller of the service you just have to call it directly.

You can consume a RESTful endpoint in .net in many ways, including:

  1. Use the WebChannelFactory
  2. Use the good old HttpClient
  3. Use WebClient

In all these instances you will need to add the [OperationContract] attribute to your service operation contract definition, otherwise the WCF runtime will not expose this operation.

-1
votes

btw, your CreateUserAccount is missing the OperationContract attribute. please add that and try the service reference again.

When you add a service reference in the COnsole Project, .NET will add named endpoint to your app.config. (the default one)

I don't see that in your app.config. Ensure you added the reference rightly.

after that you need to do,

UserServiceClient serviceClient = new UserServiceClient(); // default endpoint

OR

UserServiceClient serviceClient = new UserServiceClient(namedEndpoint); // from app.config

From your code, you're explicitly asking WCF to instantiate a client proxy with an endpoint in App.Config named blank string. ("") this is the problem.

If for some reason, your App.config doesn't have the endpoint and binding details, you can do it programmatically as well:

// programmatic binding and address

var binding = new BasicHttpBinding();
var endpointAddress = new EndpointAddress("localhost_service_address");

var serviceClient = new UserServiceClient(binding, endpointAddress);