Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF Configuration Hell?

Tags:

c#

.net

wcf

I hate WCF setup with endpoints, behaviors etc. I believe all these things should be performed automatically. All I want to do is to return JSON result from my WCF service. Here is my configuration:

<system.serviceModel>
<bindings>
  <webHttpBinding>
    <binding name="default"/>
  </webHttpBinding>
</bindings>
<services>
  <service name="HighOnCodingWebApps.ZombieService"
     behaviorConfiguration="MyServiceTypeBehaviors">
    <endpoint address="" binding="webHttpBinding"
      bindingConfiguration="default"
      contract="HighOnCodingWebApps.IZombieService"
      behaviorConfiguration="webScriptEnablingBehavior"/>
  </service>
</services>

<behaviors>
  <endpointBehaviors>
    <behavior name="webScriptEnablingBehavior">
      <enableWebScript/>
    </behavior>
  </endpointBehaviors>

  <serviceBehaviors>
    <behavior name="MyServiceTypeBehaviors">
      <serviceMetadata httpGetEnabled="true"/>


    </behavior>
  </serviceBehaviors>
</behaviors>

<serviceHostingEnvironment aspNetCompatibilityEnabled="false"/>

And I have the following service implementation:

public class ZombieService : IZombieService
    {
        [WebInvoke(Method = "GET",
                    ResponseFormat = WebMessageFormat.Json,
                    UriTemplate = "KnownZombies")]
        public Zombie GetZombie()
        {
           return new Zombie() { Name = "Mohammad Azam"};
        }
    }

    public class Zombie
    {
        public string Name { get; set; }
    }

When I visit http://localhost:22059/ZombieService/KnownZombies says the following message:

Endpoints using 'UriTemplate' cannot be used with 'System.ServiceModel.Description.WebScriptEnablingBehavior'.

If I remove the WebScriptEnablingBehavior from the web.config I get the following error:

The message with To 'http://localhost:22059/ZombieService.svc/KnownZombies' cannot be processed at the receiver, due to an AddressFilter mismatch at the EndpointDispatcher. Check that the sender and receiver's EndpointAddresses agree.

UPDATE 1:

I updated the configuration to this:

 <system.serviceModel>
    <bindings>
      <webHttpBinding>
        <binding name="default"/>
      </webHttpBinding>
    </bindings>
    <services>
      <service name="HighOnCodingWebApps.ZombieService"
         behaviorConfiguration="MyServiceTypeBehaviors">
        <endpoint address="http://localhost:22059/ZombieService.svc" binding="webHttpBinding"
          bindingConfiguration="default"
          contract="HighOnCodingWebApps.IZombieService"
          />
      </service>
    </services>

    <behaviors>
      <endpointBehaviors>
        <behavior name="SomeBehavior">
          <webHttp />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="MyServiceTypeBehaviors">

          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>

  </system.serviceModel>

Now when I visit http://localhost:22059/ZombieService.svc/KnownZombies I get the following message in the browser:

The message with To 'http://localhost:22059/ZombieService.svc/KnownZombies' cannot be processed at the receiver, due to an AddressFilter mismatch at the EndpointDispatcher. Check that the sender and receiver's EndpointAddresses agree.

like image 722
azamsharp Avatar asked Aug 11 '11 16:08

azamsharp


3 Answers

Have you tried the WCF SvcConfigEditor? It is available from the Tools menu in Visual Studio. Open your web/app.config with SvcConfigEditor to get GUI help on getting everything right.

like image 106
Anders Abel Avatar answered Nov 05 '22 04:11

Anders Abel


Take a look at this SO question.

Edit: Since you're not specifying an address for your service, try hitting: http://localhost:22059/ZombieService.svc/KnownZombies (with the .svc).

I also think you need the <webHttp /> behavior added to your specified endpoint behavior.

Edit: Try changing your endpoint definition to this:

<service 
  name="HighOnCodingWebApps.ZombieService"
  behaviorConfiguration="MyServiceTypeBehaviors">

  <endpoint 
    address="" 
    binding="webHttpBinding"
    behaviorConfiguration="SomeBehavior"
    bindingConfiguration="default"
    contract="HighOnCodingWebApps.IZombieService" />

</service>
like image 5
BrandonZeider Avatar answered Nov 05 '22 05:11

BrandonZeider


You are using the WebInvokeAttribute which tells WCF by default to accept POST as the verb. Since you are trying to access it via a GET action, it is being ignored.

Use WebGetAttribute instead.

Per MSDN:

If you want a service operation to respond to GET, use the WebGetAttribute instead.

like image 1
Josh Avatar answered Nov 05 '22 05:11

Josh