使用WCF

时间:2016-07-22 07:09:38

标签: c# asp.net wcf

Service.cs

public class StoredProcService : IStoredProcService
{

    public int addData(int x, int y)
    {

        return x + y;
    }
}
  

错误:415无法处理消息,因为内容类型   '应用/ JSON;字符集= UTF-8'不是预期的类型   '文本/ XML' ASPX

<script type="text/javascript">
  $(document).ready(function () {
      //$('input[id^="button"]').click(function () {
      //                alert('You have clicked ' + $(this).val());
      Add();
  })

  function Add() {
      alert("sds");
      $.ajax({
          type: 'POST',
          url: '/StoredProcService.svc/addData',
          contentType: 'application/json; charset=utf-8',
          dataType: 'json',
          data: {"x":"5", "y":"8"},
          success: function (data) {
              alert(data);
          }
      });
  }
</script>

标记:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js" charset="utf-8"></script>
</head>

<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" Text="Server Code" onclick="Button1_Click" />
    &nbsp;&nbsp;&nbsp;&nbsp;
        <input id="button" type="button" value="Client Side" onclick="Add()" />        
    </div>
    </form>
</body>
</html>

接口:

[ServiceContract]
public interface IStoredProcService
{

    [OperationContract]
    [WebInvoke(Method = "POST", 
               RequestFormat = WebMessageFormat.Json, 
               ResponseFormat = WebMessageFormat.Json, 
               BodyStyle = WebMessageBodyStyle.WrappedRequest, 
               UriTemplate = "addData")]
    int addData(int x, int y);
}

WebConfig:

<?xml version="1.0"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
  </system.web>
  <appSettings>`
    <add key="vs:EnableBrowserLink" value="false"/>
    <add key="erpConnectionString" value="Data Source=mssql.aksharasoftware.com,1437;Initial Catalog=erp;uid=erp;password=100_erp;Pooling=true;Connection Lifetime=0;Min Pool Size=2;Max Pool Size=400;Connection Timeout=1200;"/>
    <add key="PINEDITLICENSEKEY" value="141607143E07144807141606144804144762143E05142A06135162"/>
    <add key="owin:AutomaticAppStartup" value="false"/>
  </appSettings>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_IService" />
        <binding name="BasicHttpBinding_ISamplereturn" />
        <binding name="BasicHttpBinding_IStoredProcService" />
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://localhost:50192/Service.svc"
                binding="basicHttpBinding"
                bindingConfiguration="BasicHttpBinding_IService"
                contract="ServiceReference1.IService"
                name="BasicHttpBinding_IService" />
      <endpoint address="http://localhost:50192/Samplereturn.svc"
                binding="basicHttpBinding"
                bindingConfiguration="BasicHttpBinding_ISamplereturn"
                contract="ServiceReference2.ISamplereturn"
                name="BasicHttpBinding_ISamplereturn" />
      <endpoint address="http://localhost:50192/StoredProcService.svc"
                binding="basicHttpBinding"
                bindingConfiguration="BasicHttpBinding_IStoredProcService"
                contract="ServiceReference3.IStoredProcService"
                name="BasicHttpBinding_IStoredProcService" />
    </client>
    <behaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
                               multipleSiteBindingsEnabled="true" >
      <serviceActivations>
        <add factory="System.ServiceModel.Activation.WebServiceHostFactory"
             relativeAddress="./ServiceReference3/StoredProcService.svc"
             service="StoredProcService"/>
      </serviceActivations>
    </serviceHostingEnvironment>
  </system.serviceModel>
</configuration>

2 个答案:

答案 0 :(得分:-1)

默认情况下,所有WCF通信都使用XML进行。 尝试更正您的javascript:

function Add() {
        alert("sds");
        $.ajax({
            type: 'POST',
            url: '/StoredProcService.svc/addData',
            contentType: 'text/xml; charset=utf-8',
            dataType: 'xml',
            data: {"x":"5", "y":"8"},
            success: function (data) {
                alert(data);
            }
        });

答案 1 :(得分:-1)

将ajax调用中的内容类型更改为contentType: 'text/xml; charset=utf-8'它应该可以正常工作。

如果您收到错误400,请尝试从浏览器访问网址并更正网址。