我已经通过堆栈交换进行了搜索,虽然有很多类似的问题,但我无法解决这个问题。我需要在 Azure webrole 上运行 WCF Web 服务(因为我也在运行常规网页)而不是 Azure Web 服务角色。我已经设置了 WCF 服务并在 web.config 中定义了端点,但我无法通过浏览器或客户端连接到端点。下面是代码 - 如果有人能就可能出现的问题提出建议,我将不胜感激。
鉴于下面的代码,我假设以下代码可以在浏览器中使用
http://127.0.0.1:81/testAPI.svc/store/
但我得到一个 404 如果我只是指定
http://127.0.0.1:81/testAPI.svc
我得到的页面告诉我我有一个网络服务。
测试API.cs
namespace MvcWebRole1
{
[ServiceContract]
public interface ItestAPI
{
[OperationContract]
[WebGet(UriTemplate = "store",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare)]
List<Financials> GetStoreActuals();
}
}
测试API.svc
namespace MvcWebRole1
{
public class testAPI : ItestAPI
{
public List<Financials> GetStoreActuals()
{
return GetActuals();
}
private List<Financials> GetActuals()
{
List<Financials> Actuals = new List<Financials>
{
new Financials
{
Store = "Store1", Profit = "10000000", Sales = "2000000"
},
new Financials
{
Store = "Store2", Profit = "20000000", Sales = "30000"
},
new Financials
{
Store = "Store3", Profit = "30000000", Sales = "4000000"
},
new Financials
{
Store = "Store4", Profit = "4000000", Sales = "500000"
},
};
return Actuals;
}
}
[DataContract]
public class Financials
{
[DataMember]
public string Store { get; set; }
[DataMember]
public string Sales { get; set; }
[DataMember]
public string Profit { get; set; }
}
}
最后是web.config中的服务模型
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="servicebehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="restbehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service name ="MvcWebRole1.testAPI" behaviorConfiguration ="servicebehavior" >
<endpoint name ="RESTEndPoint"
contract ="MvcWebRole1.ItestAPI"
binding ="webHttpBinding"
address =""
behaviorConfiguration ="restbehavior"/>
</service>
</services>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
非常感谢这里的任何帮助!