.net - 如何在 IIS 上托管的 wcf 服务中使用 http 请求访问任何方法?
问题描述
我有一个 WCF 服务应用程序。我想通过 Web 浏览器上的 http 请求访问此服务。
当我在 IISExpress 上运行 WCF 服务时,它可以工作,但是当我在 IIS 上发布服务时,返回 404 错误。我该如何解决这个问题?
IService.cs
namespace Protek.WebService
{
[ServiceContract]
public interface IService
{
[WebGet(UriTemplate = "HelloWorld")]
[OperationContract]
string HelloWorld();
}
}
服务.svc
<%@ ServiceHost Language="C#" Debug="true" Service="Protek.WebService.Service" CodeBehind="Service.svc.cs" %>
服务.svc.cs
namespace Protek.WebService
{
public class Service : IService
{
public string HelloWorld()
{
return "Hello World";
}
}
}
网页配置
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="DefaultBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="WebBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="DefaultBehavior" name="Protek.WebService.Service">
<endpoint address="ws" binding="wsHttpBinding" contract="Protek.WebService.IService"/>
<endpoint address="" binding="webHttpBinding" contract="Protek.WebService.IService" behaviorConfiguration="WebBehavior"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
<host>
<baseAddresses>
<add baseAddress="http://localhost:5858/Service" />
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
解决方案
首先,stackoverflow 是基于英文的技术问答论坛,请在描述和截图中使用英文。
如果要发布 WCF REST 服务,请参考下面的回复。
如何使用 WCF 服务?
而且,当我们在IIS中托管WCF服务时,基地址是由IIS提供的,不需要在你的配置文件中添加基地址。
https://social.msdn.microsoft.com/Forums/vstudio/en-US/d42fc165-052d-476a-9580-1240b3d0293d/specify-endpoint-in-wcf-webconfig?forum=wcf
如果有,请
随时告诉我有什么我可以帮忙的。
推荐阅读
- c# - 通过对象列表更新数据库值
- docker - 可以使用 RUN 命令在 docker-compose.yml 中运行没有 Dockerfile 的命令
- excel - 如何使用 NPOI XSSF 创建 Excel 表
- ios - iOS:使用 CryptoSwift 和 SSL 时的出口合规性
- node.js - NodeJS流:WritableStream输出不一致是零字节或非零字节
- excel - 使用 VBA 将单个行粘贴到多个活动行/单元格中
- javascript - Html文本替换冻结浏览器一段时间,如何避免冻结?
- bash - bash 在超时时退出 0,在失败时退出非 0
- python - wx.EVT_MOTION 事件不适用于 wxPython 4.0.6
- cordova - 当我在 Cordova 中进行重定向时,应用程序被重定向到浏览器