从 http 和 Google Chrome 扩展简单 REST 客户端访问 WCF 服务时出现 500 间隔服务器错误



>这是我为Android设备创建的用于发布数据的服务,但此服务对我不起作用。我通过Android调用了此服务,但没有任何反应,只是在LogCat中收到-500的错误消息。我还在HTTPSIMPLE REST客户端(Google Chrome Extension)上检查了此服务,但收到错误消息。下面给出了错误消息。并且此服务也在 IIS 上发布。

错误信息

The message with Action '' cannot be processed at the receiver,due to a ContractFilter mismatch at the EndpointDispatcher. 
This may be because of either a contract mismatch (mismatched Actions between sender and receiver) or a binding/security mismatch between the sender and the receiver.
Check that sender and receiver have the same contract and the same binding (including security requirements, e.g. Message, Transport, None)

这些是我传递给服务的参数:

{"mycar":{"Name":"a","Make":"gfgfd ","Model":"web "}}

这是服务源代码

namespace CarSercive
{
[ServiceBehavior(AddressFilterMode=AddressFilterMode.Any)]
public class Service1 : IService1
{
  //      myCar test = new myCar();
    public void UpdateMyCar(myCar mycar) {
        string strConnectionString = ConfigurationManager.ConnectionStrings["Database1"].ConnectionString;
        SqlConnection conn = new SqlConnection(strConnectionString);
        conn.Open();
        using (SqlCommand cmd = new SqlCommand("Insert into TestingTable (Name,Make,Model) Values (@Name,@Make,@Model)", conn)) {
            cmd.Parameters.AddWithValue("@Name", mycar.Name);
            cmd.Parameters.AddWithValue("@Make", mycar.Make);
            cmd.Parameters.AddWithValue("@Model", mycar.Model);
            int queryResult = cmd.ExecuteNonQuery();
        } conn.Close();
    }
}
}

网络.config

<?xml version="1.0"?>
<configuration>
<appSettings/>
<connectionStrings/>
<system.web>
    <compilation debug="true" targetFramework="4.0">
    </compilation>
    <authentication mode="Windows"/>
    <pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/></system.web>
<system.serviceModel>
    <services>
        <service name="CarSercive.Service1" behaviorConfiguration="web">
            <!-- Service Endpoints -->
    <endpoint address="" binding="webHttpBinding" contract="CarSercive.IService1"></endpoint>
        </service>
    </services>
    <behaviors>
        <serviceBehaviors>
            <behavior name="web">
                <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
                <serviceMetadata httpGetEnabled="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>
  <endpointBehaviors>
    <behavior name="web">
      <webHttp/>
    </behavior>
  </endpointBehaviors>
    </behaviors>
</system.serviceModel>

IService1.cs

  namespace CarSercive
{
[ServiceContract]
public interface IService1
{
    [OperationContract]
    [WebInvoke(
        Method = "POST",
        UriTemplate = "MyCar",
        //BodyStyle = WebMessageBodyStyle.WrappedRequest,
        ResponseFormat = WebMessageFormat.Json,
        RequestFormat = WebMessageFormat.Json)]
    void UpdateMyCar(myCar mycar);
}
[DataContract]
public class myCar 
{
    [DataMember(Name = "Name")]
    public string Name
    {
        get;
        set;
    }
    [DataMember(Name="Model")]
    public string Model 
    { 
        get; 
        set; 
    }
    [DataMember(Name="Make")]
    public string Make 
    { 
        get;
        set; 
    }
}

}

您需要将 JSON 端点添加到 web.config 文件。

<system.serviceModel>
    <domainServices>
      <endpoints>
        <add name="JSON" type="Microsoft.ServiceModel.DomainServices.Hosting.JsonEndpointFactory, Microsoft.ServiceModel.DomainServices.Hosting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
      </endpoints>
    </domainServices>

确保链接到项目中的程序集 Microsoft.ServiceModel.DomainServices.Hosting.JsonEndpointFactory

最新更新