0

这听起来像是一篇非常熟悉的帖子,但我正在尝试从本地 IIS 托管的 ASP.NET WebApp 调用本地 IIS 托管的 WCF 服务。本地 Web 应用程序可以从下面的 AJAX 调用中成功“调用” .svc 文件,但只要我添加 /GetData?value=2,我就会开始收到 404 错误。

WCF 服务和 Web 应用程序都托管在 IIS7 的 localhost 上,因此我不应该遇到任何跨域问题。

很抱歉,同样的问题已经被问了大约 1 亿次,而我只是另一个试图让它工作的 WCF 菜鸟,但我们将不胜感激任何帮助。我已经在谷歌上搜索并尝试了大约一天的不同事情,我终于提交了。

谢谢,杰森

这是我的服务合同:

namespace WcfService1
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IService1
    {

        [OperationContract]
        [WebInvoke(Method="GET", ResponseFormat=WebMessageFormat.Json)]
        string GetData(int value);

        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);

        // TODO: Add your service operations here
    }


    // Use a data contract as illustrated in the sample below to add composite types to service operations.
    [DataContract]
    public class CompositeType
    {
        bool boolValue = true;
        string stringValue = "Hello ";

        [DataMember]
        public bool BoolValue
        {
            get { return boolValue; }
            set { boolValue = value; }
        }

        [DataMember]
        public string StringValue
        {
            get { return stringValue; }
            set { stringValue = value; }
        }
    }
}

这是服务实现:

namespace WcfService1
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    [AspNetCompatibilityRequirements(RequirementsMode=AspNetCompatibilityRequirementsMode.Allowed)]
    public class Service1 : IService1
    {
        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }

        public CompositeType GetDataUsingDataContract(CompositeType composite)
        {
            if (composite == null)
            {
                throw new ArgumentNullException("composite");
            }
            if (composite.BoolValue)
            {
                composite.StringValue += "Suffix";
            }
            return composite;
        }
    }
}

这是服务 web.config:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>

  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="default">
          <!-- 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="httpEndPoint">
                <webHttp />
                <!--<enableWebScript />-->
            </behavior>
        </endpointBehaviors>
    </behaviors>
      <services>
          <service behaviorConfiguration="default" name="Service">
              <endpoint address="" binding="webHttpBinding" contract="WcfService1.IService1" behaviorConfiguration="httpEndPoint"></endpoint>
              <endpoint address="mex" binding="webHttpBinding" contract="IMetadataExchange" behaviorConfiguration="httpEndPoint"></endpoint>
          </service>
      </services>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
        <defaultDocument>
            <files>
                <add value="Service1.svc" />
            </files>
        </defaultDocument>
  </system.webServer>

</configuration>

这是我的 AJAX 调用:

    $(document).ready(
        function()
        {
            $.ajax(
                {
                    type: "GET",
                    url: "http://localhost/testservice/GetData?value=2",
//                    contentType: "application/json; charset=utf-8",
//                    DataType: "jason",
                    success: function() { alert("success"); },
                    error: function(result) { alert(result.status + " " + result.statusText); }
                }
            );
        }
    );
4

1 回答 1

0

由于配置中的 webHttp 标签,您已经设置了休息服务。

代替

http://localhost/testservice/GetData?value=2

http://localhost/testservice/GetData/2
于 2012-09-08T00:22:34.193 回答