我按照这个在 asp.net 3.5 中创建了一个 json web 服务:
(位于:http ://www.pluralsight.com/community/blogs/fritz/archive/2008/01/31/50121.aspx )
如果我想在内部使用它,它工作正常,但当我想在外部连接到它时,我收到一条错误消息:“此服务的元数据发布当前已禁用。”
所以我尝试启用它,但现在我收到错误消息:“无法将 'serviceMetadata' 行为扩展添加到 'MyServiceAspNetAjaxBehavior' 端点行为,因为底层行为类型未实现 IEndpointBehavior 接口。”。
我知道我在 web.config 中做错了什么,只是想不通,我做错了什么?谢谢!
这是在 web.config 中:
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="MyServiceAspNetAjaxBehavior">
<enableWebScript />
<serviceMetadata httpGetEnabled="true" />
</behavior>
</endpointBehaviors>
</behaviors>
//Needed to add this to be able to use the web service on my shared host
<serviceHostingEnvironment aspNetCompatibilityEnabled="true">
<baseAddressPrefixFilters>
<add prefix="http://www.domain.com"/>
</baseAddressPrefixFilters>
</serviceHostingEnvironment>
<services>
<service name="MyService">
<endpoint address="" behaviorConfiguration="MyServiceAspNetAjaxBehavior" binding="webHttpBinding" contract="MyService" />
<endpoint contract="MyService" binding="mexHttpBinding" address="mex" />
</service>
</services>
在 MyService.cs 中:
using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class MyService
{
[OperationContract]
public string GetForecast(string str)
{
return "Hello World";
}
}
在 MyService.svc 中
<%@ ServiceHost Language="C#" Debug="true" Service="MyService" CodeBehind="~/App_Code/MyService.cs" %>