0

I'm a newbie so please be patient, I've created a simple WCF method and this is my interface

public interface IService1
    {
        [OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "DoWork", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        string DoWork();
    }

This is my class

public class Service1 : IService1
    {
        public string DoWork()
        {
            return "Hello World!";
        }

With Postman I make a POST request, sending a raw body with {} as parameters and type JSON (application/json), calling

http://localhost:50796/Service1.svc/DoWork

And I get the following error

415 cannot process the message beacuse the content type 'application/json' was not the expected type 'text/xml; charset=utf-8'

UPDATE This is my web.config

<?xml version="1.0"?>
<configuration>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.6.1" />
    <httpRuntime targetFramework="4.6.1"/>
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true"/>
  </system.webServer>

</configuration>

Why? Can you help me? Thanks

blies
  • 31
  • 6
  • Also tried to change the request type to text/xml and I get 400 Bad Request – blies Oct 14 '21 at 04:52
  • Where do you set up the channel; either in code or using web.config? `[WebInvoke]` requires the WebHttpBehavior as per this [documentation](https://learn.microsoft.com/en-us/dotnet/api/system.servicemodel.web.webinvokeattribute): `Applying the WebInvokeAttribute attribute to a service operation has no effect unless a behavior that looks for this metadata in the operation description (such as WebHttpBehavior) is added to the service's behavior collection.` – Tom W Oct 14 '21 at 07:28
  • I update the post with my web.config, but now I get error 500 System.ServiceModel.ActivationException – blies Oct 14 '21 at 08:11
  • Instead of writing code based on [the tutorial](https://learn.microsoft.com/en-us/dotnet/framework/wcf/how-to-host-and-run-a-basic-wcf-service), you can try to run it first. – Jiayao Oct 14 '21 at 08:28
  • Your request is a post and sending JSON. The issue is in the Service receiving the request with a GET. The service is expecting XML and you sent json. – jdweng Oct 14 '21 at 09:03
  • @jdweng sorry .. where in the web.config is specified that the service have to receive with a GET? How can I fix this to allow my external web applications to POST with a json? – blies Oct 14 '21 at 10:27
  • 1
    @blies you need to configure the service with a WebHttpBinding otherwise the service will boot up assuming the WCF default, which is SOAP. The top answer to [this question](https://stackoverflow.com/questions/17644392/configuring-wcf-rest-services-in-web-config) explains how to do this in configuration. – Tom W Oct 14 '21 at 11:05
  • @jdweng this is nothing to do with MVC. – Tom W Oct 14 '21 at 12:23
  • @TomW: The controller is buried in the MVC classes. The POST/GET is the same for MVC and controller. – jdweng Oct 14 '21 at 12:41
  • See following : https://learn.microsoft.com/en-us/dotnet/api/system.servicemodel.web.webgetattribute.requestformat?view=netframework-4.8#System_ServiceModel_Web_WebGetAttribute_RequestFormat – jdweng Oct 14 '21 at 13:11
  • @jdweng No. OP didn't mention MVC at all. This is a problem with a WCF service. – Tom W Oct 14 '21 at 13:13

0 Answers0