开发者

I've created a WCF service and now I want to call it from a reference but why InvalidOperationException?

I've written a WCF service. I have successfully browsed to the service and it says:

You have created a service.

So then I add a reference to it using a the 'Add Service Reference' in visual studio. Then I write the following code to consume it....

ServiceReference1.VLSContentServiceClient client = new ServiceReference1.VLSContentServi开发者_如何学JAVAceClient("VLSContentServiceEndpointBehaviour");
List<ServiceReference1.Category> cats = client.GetCategoriesByGET();

But im getting the error:

Could not find endpoint element with name 'VLSContentServiceEndpointBehaviour' and contract 'ServiceReference1.IVLSContentService' 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 name could be found in the client element.

It makes no sense because the argument 'endPointConfigurationName' matches what I have set it to in the service. Here is the service configuration:

  <system.serviceModel>

    <services>
      <service behaviorConfiguration="VLSContentServiceBehaviour" name="VLSContentService">
        <endpoint address="" behaviorConfiguration="VLSContentServiceEndpointBehaviour" binding="webHttpBinding" contract="IVLSContentService"/>
      </service>
    </services>

    <behaviors>
      <serviceBehaviors>
        <behavior name="VLSContentServiceBehaviour">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>

      <endpointBehaviors>
        <behavior name="VLSContentServiceEndpointBehaviour">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>

  </system.serviceModel>

Whats going on?


You are using REST service - client for such service cannot be created with Add service reference. That is only for SOAP services (no webHttpBinding and webHttp behavior). Also once you use SOAP service you don't pass name of any server side feature to the constructor of the proxy. The proxy constructor expects name of client endpoint from client's configuration.

How to consume REST service


Looks like your contract is not correct. you have:

contract="IVLSContentService"/>

and it is expecting:

contract="ServiceReference1.IVLSContentService"/>

according to the error message.

also your endpoint address is empty. Does that not need to contain something?


The VLSContentServiceEndpointBehaviour parameter while creating an instance of client is the Name of the endpoint and not the endpoing behaviour.

Change

    <endpoint address="" behaviorConfiguration="VLSContentServiceEndpointBehaviour" binding="webHttpBinding" contract="IVLSContentService"/>

to

    <endpoint address="" name ="Client" behaviorConfiguration="VLSContentServiceEndpointBehaviour" binding="webHttpBinding" contract="IVLSContentService"/>

and create a service client as

ServiceReference1.VLSContentServiceClient client = new ServiceReference1.VLSContentServiceClient("Client");

Also your address is missing which is bit strange.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜