在 ASP.NET MVC 2.0 中配置简单的 WCF 服务端点

发布于 2024-09-25 04:04:48 字数 1365 浏览 0 评论 0原文

我正在构建一个简单的 ASP.NET MVC 2.0 Web 应用程序。我想提供一个 AtomPub 端点,以便我可以从 Windows Live Writer 发布/更新内容。我最初选择将 AtomPub 协议实现为具有一组自定义 ActionResults 的控制器。这种方法一直有效,直到我尝试让身份验证正常工作,当时我意识到在基于 Forms-Auth 的 MVC 应用程序中使用基本或摘要身份验证(WLW 所必需的)将会出现问题。

因此,现在我决定将 AtomPub 逻辑移至 MVC 应用程序中的 WCF 服务。我创建了一个名为 AtomPub.svc 的新 WCF 服务。我将以下内容添加到我的 web.config 文件中:

<system.serviceModel>
  <behaviors>
    <serviceBehaviors>
      <behavior name="">
        <serviceMetadata httpGetEnabled="true" />
        <serviceDebug includeExceptionDetailInFaults="false" />
      </behavior>
    </serviceBehaviors>
  </behaviors>
</system.serviceModel>

而且,我的 AtomPub.svc.cs 后面的代码如下所示:

namespace Web
{
    using System.ServiceModel;
    using System.ServiceModel.Web;

    [ServiceContract]
    public partial class AtomPub
    {
        [WebGet(UriTemplate = "?test={test}")]
        [OperationContract]
        public string DoWork(string test)
        {
            return test;
        }
    }
}

我还添加了一个路由排除,以将此端点从 MVC 的路由处理中排除。

现在,我对 WCF 完全是个菜鸟,所以我确信我做错了很多事情。当我写这篇文章时,当我获得 AtomPub 服务页面时,端点的根似乎正在工作。但是,URL 模板无法正常工作,我不知道该怎么做才能使其正常工作。我很想听听你的建议。

顺便说一句,我试图使整个实现尽可能简单。因此,我不想引入对实体框架的依赖关系,以便我可以使用 WCF 数据服务。我也不想将 WCF 端点移至单独的项目中,但如果我可以轻松地将其部署到 W2k3/IIS6 环境,我对此持开放态度。

I'm building a simple ASP.NET MVC 2.0 web application. I'd like to serve up a AtomPub endpoint so that I can publish/update content from Windows Live Writer. I originally went down the path of implementing the AtomPub protocol as an Controller with a set of custom ActionResults. That worked until I tried to get authentication working, when I realized that getting Basic or Digest auth (necessary for WLW) to work within my Forms-Auth based MVC app was going to be problematic.

So, now I've decided to move the AtomPub logic to a WCF service within the MVC app. I created a new WCF service called AtomPub.svc. I added the following to my web.config file:

<system.serviceModel>
  <behaviors>
    <serviceBehaviors>
      <behavior name="">
        <serviceMetadata httpGetEnabled="true" />
        <serviceDebug includeExceptionDetailInFaults="false" />
      </behavior>
    </serviceBehaviors>
  </behaviors>
</system.serviceModel>

And, my AtomPub.svc.cs code behind looks like this:

namespace Web
{
    using System.ServiceModel;
    using System.ServiceModel.Web;

    [ServiceContract]
    public partial class AtomPub
    {
        [WebGet(UriTemplate = "?test={test}")]
        [OperationContract]
        public string DoWork(string test)
        {
            return test;
        }
    }
}

I also added a route exclusion to exclude this endpoint from MVC's route handling.

Now, I'm a total noob to WCF, so I'm sure I'm doing a number of things wrong. As I'm writing this, the root of the endpoint seems to be working as I get an AtomPub Service page. The URL templating, however, is not working and I don't know what to do to get it working. I'd love to hear your suggestions.

By the way, I'm trying to keep this overall implementation as simple as possible. So, I'm not looking to introduce a dependency on the Entity Framework so I can use WCF Data Services. I'd also prefer not to move the WCF endpoint into a separate project, though I'm open to that if I can easily deploy it to a W2k3/IIS6 environment.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文