【问题标题】:Is there a way to have custom defined friendly URLs with WCF without IIS?有没有办法在没有 IIS 的情况下使用带有 WCF 的自定义友好 URL?
【发布时间】:2011-03-06 15:23:55
【问题描述】:

有没有办法在没有 IIS 的情况下使用 WCF 自定义友好 URL?

特别是我希望通过我自己的 Windows 服务中托管的 app.config 来做这样的事情:

[WebGet(UriTemplate = "foo/{id}")]
public string GetFoo(string id)
{
   ...
}

【问题讨论】:

    标签: c# .net wcf config


    【解决方案1】:

    是的,当然 - 在您自己的 NT 服务中托管它,并在您的 app.config 中定义一个 http 基地址。您在服务合同中定义的 URI 模板将不在该基址:

    <services>
       <service name="YourNamespace.YourServiceClass">
          <host>
             <baseAddresses>
                 <add baseAddress="http://YourServer:9091/Services/" />
             </baseAddresses>
          </host>
          <endpoint address="" 
                    binding="webHttpBinding" 
                    contract="YourNamespace.IYourService" />    
       </service>
    </services>
    

    然后你的 URI 模板将被添加到这个基地址,所以在这种情况下,你的 GetFoo 方法可以在以下位置调用:

    http://YourServer:9091/Services/foo/42
    

    更新:我刚刚在这里重新创建了这个,问题是这样的:你的 URI 模板定义了一个名为 {id} 的参数,但是你应用这个 URI 模板的方法没有任何参数在它的参数列表中调用id

    [WebGet(UriTemplate = "foo/{id}")]
    public string GetFoo(string fooID)
    

    您需要确保这些东西匹配!使用给定的 URI 模板,您需要将方法声明更改为:

    [WebGet(UriTemplate = "foo/{id}")]
    public string GetFoo(string id)
    

    (查看参数 - 它的名称现在是 id)然后你应该没问题 - 至少它对我来说很好。

    【讨论】:

    • 我试过了,但它仍然不使用我的自定义 WebGet UriTemp。仅当我在 IIS 中托管服务 DLL 或通过 F5 Visual Studio 调试没有 Windows 服务时。我认为使它适用于 IIS 的设置是:&lt;system.webServer&gt; &lt;modules runAllManagedModulesForAllRequests="true"&gt; &lt;add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b13f5f7f11d50a3a" /&gt; &lt;/modules&gt; &lt;/system.webServer&gt;
    • 顺便说一下我用的是VS2010和C#
    • @wcf 大师需要:更新我的答案 - 我希望这是你遇到的问题 - 至少它现在对我有用。
    • 谢谢 这对我通过网络浏览器调用 URL 有效吗?由于 EndpointDispatcher 的 ContractFilter 不匹配,我收到 Sendera:ActionNotSupportedThe message with Action '' 无法在接收方处理。这可能是因为合约不匹配(发送方和接收方之间的操作不匹配)或发送方和接收方之间的绑定/安全不匹配。检查发送方和接收方是否具有相同的合同和相同的绑定(包括安全要求,例如消息、传输、无)。
    • 如果您能帮助我解决这里的新错误,我将不胜感激,非常感谢您迄今为止的帮助:stackoverflow.com/questions/5211909/…
    猜你喜欢
    • 2014-05-11
    • 1970-01-01
    • 2016-01-10
    • 2016-01-19
    • 1970-01-01
    • 2015-07-11
    • 1970-01-01
    • 1970-01-01
    • 2012-03-16
    相关资源
    最近更新 更多