当托管在 IIS 中时,如何通过代码配置 WCF 服务?

发布于 2024-10-04 18:23:18 字数 296 浏览 1 评论 0原文

我的 WCF 服务公开 https 和 http 端点。除了 SSL 之外,它们是相同的。它们映射到相同的代码。

最终目的是外部用户通过https连接,内部用户使用http。

在开发中这给我带来了一个问题。 Cassini(VS 中打包的开发 Web 服务器)讨厌 SSL。

我想知道是否可以通过代码配置服务,因此在 Cassini 下运行时,我不会配置 https。

因此,问题是:如果服务是 IIS 托管的,如何通过代码配置该服务?我很高兴能得到关于如何说服卡西尼号不要抱怨配置的 https 部分的替代答案。

My WCF service exposes an https AND an http endpoint. Apart from the SSL they are identical. They map to the same code.

The ultimate intention is for external users to connect via https, internal users to use http.

In development this gives me a problem. Cassini, the development web server packaged in VS, hates SSL.

I'm wondering if I can configure the service from code, so when running under Cassini, I would not configure https.

Hence the question - How do I configure the service from code if it is IIS hosted? I'd be very happy with alternative answers on how I can persuade Cassini to NOT complain about the https part of the configuration.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

静谧幽蓝 2024-10-11 18:23:18

“IIS 将负责根据您的 *.svc 文件启动必要的 ServiceHost - 实际上您对此无能为力。”

不太接近事实。正是在您的服务的 SVC 文件中,有一个名为 Factory 的属性。您可以在其中指定类以及该类所在的程序集。这个类可能是您自己的 Web|DataServiceHostFactory 的后代
因此,您的 svc 标记将如下所示

<%@ ServiceHost 
Language="C#"
 Debug="true" 
 Service="name.space.myService" 
 CodeBehind="name.space.myService.svc.sc" 
 Factory = "name.space.WebServiceHostFactoryEx, assembly.name"
 %>

WebServiceHostFactory 将为每个服务命中创建,并将按照您想要的方式重新创建主机。

您还需要继承 WebServiceHost 并按照您需要的方式创建它,并具有某些端点、行为、地址等设置 - 无论您喜欢什么。

Michele Bustamante 这里有一篇非常好的文章< /a>

编辑:我发现上面的链接不再起作用,所以这里是 另一个

我在 IIS 托管环境中使用它来实现几个以相同方式初始化的服务。

"IIS will take care of spinning up the necessary ServiceHost based on your *.svc file - not a whole lot you can do about that, really."

Not too close to the truth. Exactly in the SVC file of your service there is attribute named Factory. Where you can specify the the class and the assebly where the class is located. This class may be your own descendant of Web|DataServiceHostFactory
So your svc markup would look like this

<%@ ServiceHost 
Language="C#"
 Debug="true" 
 Service="name.space.myService" 
 CodeBehind="name.space.myService.svc.sc" 
 Factory = "name.space.WebServiceHostFactoryEx, assembly.name"
 %>

The WebServiceHostFactory will be created for every service hit and will recreate your host the way you want it.

You will also need to inherith WebServiceHost and create it the way you need it with certain endpoins, behaviors, addresses, etc settings - whatever you like.

There is very nice post from Michele Bustamante here

EDIT: I figured out the above link is not working anymore, so here it is another one.

I am using this in IIS hosted enviroment for couple of services that are initialized same way.

失与倦" 2024-10-11 18:23:18

当您在 IIS 中托管时,您需要对 IIS 领域进行大量关注 - 在这种情况下您无法真正掌握您的服务。

IIS 将根据您的 *.svc 文件负责启动必要的 ServiceHost - 实际上您对此无能为力。

我的解决方案会有所不同 - 在配置文件 (web.config) 中外部化 标记:

<system.serviceModel>
  <services>      
     <service configSource="service.dev.config" />
  </services>
</system.serviceModel>

在您的开发环境中,仅公开 http 端点 - 所以您的service.dev.config 看起来像这样:

<service name=".....">
    <endpoint name="default"
              address="....."
              binding="basicHttpBinding" bindingConfiguration="insecure"
              contract="......" />
</service>

创建第二个 service.prod.config,其中包含两个端点 - http 和 https:

<service name=".....">
    <endpoint name="default"
              address="....."
              binding="basicHttpBinding" bindingConfiguration="insecure"
              contract="......" />
    <endpoint name="secure"
              address="....."
              binding="basicHttpBinding" bindingConfiguration="secure"
              contract="......" />
</service>

并在您的 中引用它>web.config 在部署服务器上。

When you're hosting in IIS, you're leaving a lot of care taking into the realm of IIS - you cannot really grab a hold of your service in this case.

IIS will take care of spinning up the necessary ServiceHost based on your *.svc file - not a whole lot you can do about that, really.

My solution would be different - externalize the <service> tag in your configuration file (web.config):

<system.serviceModel>
  <services>      
     <service configSource="service.dev.config" />
  </services>
</system.serviceModel>

In your dev environment, only expose the http endpoint - so your service.dev.config would look something like this:

<service name=".....">
    <endpoint name="default"
              address="....."
              binding="basicHttpBinding" bindingConfiguration="insecure"
              contract="......" />
</service>

Create a second service.prod.config which then contains both endpoints - http and https:

<service name=".....">
    <endpoint name="default"
              address="....."
              binding="basicHttpBinding" bindingConfiguration="insecure"
              contract="......" />
    <endpoint name="secure"
              address="....."
              binding="basicHttpBinding" bindingConfiguration="secure"
              contract="......" />
</service>

and reference that in your web.config on the deployment server.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文