简体   繁体   English

当自托管主机应用程序甚至无法使用WCF服务本身时定义服务端点的意义是什么?

[英]what is the point of defining service endpoints in a WCF service itself when a self-hosted host application can't even use it

I am totally new to WCF and I am still learning the basics. 我完全不熟悉WCF,但仍在学习基础知识。 What I've learned so far was that I can create a service and configure its endpoints and behaviors in the service's config file. 到目前为止,我了解到的是,我可以创建一个服务,并在该服务的配置文件中配置其端点和行为。 And when I run my service through visual studio, a default application will be created and the service will be hosted in IIS successfully and everything works great. 当我通过Visual Studio运行服务时,将创建一个默认应用程序,并且该服务将成功托管在IIS中,并且一切正常。

Now when I create a host application for my service, I figured out that I should add the service endpoints (and behaviors) for my service again in the code as following: 现在,当我为服务创建主机应用程序时,我发现应该在代码中再次为服务添加服务端点(和行为),如下所示:

ServiceHost host = new ServiceHost(typeof(HelloService));
host.AddServiceEndpoint(typeof(IHelloWorld), 
                        new WSHttpContextBinding(), 
                        "http://localhost:8873/helloworld/ws");

host.Open();

foreach (var se in host.Description.Endpoints)
{
    Console.WriteLine(se.Address);
}

host.Close();
Console.Read();

or I can do it in the host application's config file 或者我可以在主机应用程序的配置文件中完成此操作

So here are my question : 所以这是我的问题:

  1. what is the point of defining endpoints in service's own config file when it is not even useful in a host application? 当服务本身的配置文件在主机应用程序中甚至没有用时,定义端点的意义何在?

  2. Or is it that service's config file only applies to IIS and managed hosts only? 还是服务的配置文件仅适用于IIS和托管主机?

  3. and finally is there a way to have the service's own configurations in the host application (not defining the endpoints and behaviors in the host application again) or the two mentioned configurations are completely different? 最后有没有办法在主机应用程序中拥有服务自己的配置(不再在主机应用程序中定义端点和行为),或者上述两种配置完全不同?

EDIT 编辑

my ultimate question is that how can I use the configurations defined in service's config file in the host application?(Without using host application's own config file or creating additional code to define new endpoints and behaviors ) 我的最终问题是,如何使用宿主应用程序中服务的配置文件中定义配置?(不使用宿主应用程序自己的配置文件或创建其他代码来定义新的端点和行为)

Now when I create a host application for my service, I figured out that I should add the service endpoints (and behaviors) for my service again in the code 现在,当我为服务创建主机应用程序时,我发现应该在代码中再次为服务添加服务端点(和行为)

This is incorrect. 这是不正确的。 You don't have to define your service endpoint in code at all. 您根本不必在代码中定义服务端点。 That is what the config file is for. 这就是配置文件的用途。

Simply pass in the name of your service (as defined in your config file) as a type into your servicehost contructor: 只需将服务名称(在配置文件中定义)作为类型传递给servicehost构造器:

var host = new ServiceHost(typeof(MyNamespace.MyService));

With the config defined as: 配置定义为:

<system.serviceModel>
    <services>
      <service name="MyNamespace.MyService" behaviorConfiguration="MyServiceBehavior">
        <endpoint address="MyService"
                  binding="basicHttpBinding"
                  contract="MyNamespace.IMyService" />

        <endpoint address="mex"
                  binding="mexHttpBinding"
                  contract="IMetadataExchange" />

        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8456/"/>
          </baseAddresses>
        </host>
      </service>
    </services>

    <behaviors>
      <serviceBehaviors>
        <behavior name="MyServiceBehavior">
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>

  </system.serviceModel>

WCF will work out that you want to use the config file to define the service to run. WCF将计算出您要使用配置文件来定义要运行的服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM