简体   繁体   中英

Using WCF services as Restful Architecture

I am trying to convert my wcf services to a restful architecture. I started by converting a service called ConnectToApplication which checks user permission for authentification purposes in order to support POST requests. While trying Ajax to query the service using the following javascript code:

   var formData={"userName":"admin", "password":"act;2016@","ApplicationName":"actior"}


        $.ajax({
            url : "http://localhost:10220/AdministrationService/ConnectToApplication",
            type: "POST",
            data: formData,



            success: function (data) { 
            alert('success');
            },
              error: function(XMLHttpRequest, textStatus, errorThrown) { 
                           alert("Status: " + textStatus); alert("Error: " + errorThrown); 
               } 

        });

I got an HTTP 400(Bad request) with the following error:

The server encountered an error processing the request. The exception message is 'The incoming message has an unexpected message format 'Raw'. The expected message formats for the operation are 'Xml'; 'Json'. This can be because a WebContentTypeMapper has not been configured on the binding. See the documentation of WebContentTypeMapper for more details.'. See server logs for more details. The exception stack trace is:

at System.ServiceModel.Dispatcher.DemultiplexingDispatchMessageFormatter.DeserializeRequest(Message message, Object[] parameters) at System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DeserializeRequest(Message message, Object[] parameters) at System.ServiceModel.Dispatcher.CompositeDispatchFormatter.DeserializeRequest(Message message, Object[] parameters) at System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs(MessageRpc& rpc) at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(Messa geRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc& rpc) at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)

Here is my metadata for endpoint configuration:

  [OperationContract]
         [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest, UriTemplate = "ConnectToApplication/")]

        UserPermessionDTO ConnectToApplication(string userName, string password, string ApplicationName); 

Here is my WCF configuration:

    <services>
      <service name="ActiorServeurConsoleApp.AdministrationService" behaviorConfiguration="serviceBehavior">
            <endpoint address="" binding="webHttpBinding" contract="ActiorServeurConsoleApp.IAdministrationService"
                      behaviorConfiguration="web">
              <identity>
                <dns value="localhost" />
              </identity>
            </endpoint>

            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
            <host>
              <baseAddresses>
                <add baseAddress="http://localhost:10220/AdministrationService/" />
              </baseAddresses>
            </host>
          </service>
    <services>


    <serviceBehaviors>
      <behavior name="serviceBehavior">
              <serviceMetadata httpGetEnabled="false"/>
              <serviceDebug includeExceptionDetailInFaults="true"/>
            </behavior>
     </serviceBehaviors>

  <behavior name="serviceBehavior">
          <serviceMetadata httpGetEnabled="false"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>

You can try to set the content type for the post as

contentType: "application/json; charset=utf-8",

Also you can look at tools like fiddler and possibly postman to check you types and also test your service.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM