这听起来像是一个非常熟悉的帖子,但我试图从本地IIS托管的ASP.NET WebApp调用本地IIS托管的WCF服务。本地网络应用程序可以成功地呼叫"来自下面的AJAX调用的.svc文件,但是当我添加/ GetData?value = 2时,我开始收到404错误。
WCF服务和Web应用程序都托管在IIS7中的localhost上,因此我不应该遇到任何跨域问题。
我很抱歉这个问题已被问过大约1亿次,我只是另一个WCF noob试图让它工作,但任何帮助都将非常感激。我现在一直在谷歌搜索和尝试不同的事情,我终于提交了。
谢谢, 杰森
这是我的服务合同:
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); }
}
);
}
);
答案 0 :(得分:0)
由于配置中的webHttp标记,您可以设置休息服务。
而不是
http://localhost/testservice/GetData?value=2
待办事项
http://localhost/testservice/GetData/2