简体   繁体   English

使用WCF服务作为Restful体系结构

[英]Using WCF services as Restful Architecture

I am trying to convert my wcf services to a restful architecture. 我正在尝试将我的wcf服务转换为宁静的体系结构。 I started by converting a service called ConnectToApplication which checks user permission for authentification purposes in order to support POST requests. 我首先转换了一个名为ConnectToApplication的服务,该服务检查用户权限以进行身份​​验证以支持POST请求。 While trying Ajax to query the service using the following javascript code: 在尝试Ajax使用以下JavaScript代码查询服务时:

   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: 我收到HTTP 400(错误请求),并显示以下错误:

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'; 该操作的预期消息格式为“ Xml”; 'Json'. “杰森”。 This can be because a WebContentTypeMapper has not been configured on the binding. 这可能是因为尚未在绑定上配置WebContentTypeMapper。 See the documentation of WebContentTypeMapper for more details.'. 有关更多详细信息,请参见WebContentTypeMapper的文档。 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 在System.ServiceModel.Dispatcher.DemultiplexingDispatchMessageFormatter.DeserializeRequest(消息,Object []参数)在System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DeserializeRequest(消息,Object []参数)在System.ServiceModel.Dispatcher.CompositeDispatchFormatter.DeserializeRequest(消息消息,位于System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs(MessageRpc&rpc)的System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&rpc)的System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.Process5 System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageIpc.Dispatcher.ImmutableDispcherRun.ProcessMessage41(MessageRpc.rpc) 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) System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc&rpc)处的geRpc&rpc)System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc&rpc)处的System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc&rpc) Dispatcher.MessageRpc.Process(布尔值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: 这是我的WCF配置:

    <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", 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. 您还可以查看诸如提琴手和邮递员之类的工具,以检查类型并测试服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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