【问题标题】:Configuring self hosted wcf service endpoints配置自托管 wcf 服务端点
【发布时间】:2012-04-13 08:18:47
【问题描述】:

我举了一个最基本的例子来设置一个托管端点...... @ 987654321@ ...从这里我想让我的服务以它在IIS中的方式配置......使用我的应用程序的配置文件。

这似乎不是这种情况下的默认设置。

有什么想法吗?

编辑:

根据上面的链接,我有类似的东西......

// Create the ServiceHost.
using (ServiceHost host = new ServiceHost(typeof(HelloWorldService), baseAddress))
{
    // Enable metadata publishing.
    ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
    smb.HttpGetEnabled = true;
    smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
    host.Description.Behaviors.Add(smb);

    // Open the ServiceHost to start listening for messages. Since
    // no endpoints are explicitly configured, the runtime will create
    // one endpoint per base address for each service contract implemented
    // by the service.
    host.Open();

    Console.WriteLine("The service is ready at {0}", baseAddress);
    Console.WriteLine("Press <Enter> to stop the service.");
    Console.ReadLine();

    // Close the ServiceHost.
    host.Close();
}

现在我想使用配置分配“baseAddress”和 smb 对象信息。 例如定义在http://msdn.microsoft.com/en-us/library/ms733932.aspx ...

<system.ServiceModel>

   <services>
   <!—- Define the service endpoints. This section is optional in the new
    default configuration model in .NET Framework 4. -->
      <service>
         <endpoint/>
      </service>
   </services>

   <bindings>
   <!-- Specify one or more of the system-provided binding elements,
    for example, <basicHttpBinding> --> 
   <!-- Alternatively, <customBinding> elements. -->
      <binding>
      <!-- For example, a <BasicHttpBinding> element. -->
      </binding>
   </bindings>

   <behaviors>
   <!-- One or more of the system-provided or custom behavior elements. -->
      <behavior>
      <!-- For example, a <throttling> element. -->
      </behavior>
   </behaviors>

</system.ServiceModel>

我的问题是,如果我使用浏览器浏览到配置的基地址,端点就不会像预期的那样存在。

我没有收到任何错误,而且它的行为就像有一个活动端点......但是它在哪里?

编辑 2:附加信息: 我使用的代码如上,我的配置文件是这样的......

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
    <section name="TaskServiceConfiguration" type="emedia.nemo.Configuration.XmlSerializerSectionHandler, emedia.nemo"/>
  </configSections>

  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>

  <TaskServiceConfiguration type="Emedia.TaskScheduler.Service.TaskServiceConfiguration, Emedia.TaskScheduler.Service, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
    <PollInterval>5</PollInterval>
    <ServiceURL>http://localhost:10000/TaskSchedulerService.svc</ServiceURL>
  </TaskServiceConfiguration>

  <log4net>
    <appender name="FileAppender" type="log4net.Appender.FileAppender">
      <file value="log-file.txt"/>
      <appendToFile value="true"/>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date %-5level %logger - %message%newline"/>
      </layout>
    </appender>
    <appender name="DebugFileAppender" type="log4net.Appender.FileAppender">
      <file value="log-file.txt"/>
      <appendToFile value="true"/>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date %-5level %logger - %message%newline"/>
      </layout>
    </appender>
    <appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">
      <mapping>
        <level value="ERROR"/>
        <foreColor value="Red"/>
      </mapping>
      <mapping>
        <level value="DEBUG"/>
        <foreColor value="Yellow"/>
      </mapping>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%-5level- %message%newline"/>
      </layout>
    </appender>
    <root>
      <level value="DEBUG"/>
      <appender-ref ref="FileAppender"/>
      <appender-ref ref="ColoredConsoleAppender"/>
    </root>
  </log4net>

  <system.diagnostics>
    <sources>
      <source name="System.ServiceModel.MessageLogging" switchValue="Warning, ActivityTracing">
        <listeners>
          <add name="Default" type="System.Diagnostics.DefaultTraceListener" />
          <add name="ServiceModelMessageLoggingListener" />
        </listeners>
      </source>
      <source name="System.ServiceModel" propagateActivity="true" switchValue="Warning, ActivityTracing">
        <listeners>
          <add name="Default" type="System.Diagnostics.DefaultTraceListener" />
          <add name="ServiceModelTraceListener" />
        </listeners>
      </source>
    </sources>
    <sharedListeners>
      <add name="ServiceModelMessageLoggingListener"
           initializeData="Web_messages.svclog"
           traceOutputOptions="Timestamp"
           type="System.Diagnostics.XmlWriterTraceListener, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
           />
      <add name="ServiceModelTraceListener"
           initializeData="Web_tracelog.svclog"
           traceOutputOptions="Timestamp"
           type="System.Diagnostics.XmlWriterTraceListener, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
           />
    </sharedListeners>
  </system.diagnostics>

  <system.serviceModel>
    <!-- Server side stuff -->
    <services>
      <service behaviorConfiguration="wsHttpBehaviour"
               name="Emedia.Messaging.Services.TaskSchedulerService">
        <endpoint address="http://localhost:10000/TaskSchedulerService.svc"
                  binding="wsHttpBinding"
                  bindingConfiguration="wsHttpBinding"
                  contract="Emedia.Messaging.Services.ITaskServiceContract"
                  />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="wsHttpBehaviour">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>

    <!-- Client side stuff -->
    <bindings>
      <wsHttpBinding>
        <binding name="wsHttpBinding"
                 closeTimeout="00:01:00"
                 openTimeout="00:01:00"
                 receiveTimeout="00:10:00"
                 sendTimeout="00:01:00"
                 bypassProxyOnLocal="false"
                 transactionFlow="false"
                 hostNameComparisonMode="StrongWildcard"
                 maxBufferPoolSize="524288"
                 maxReceivedMessageSize="65536"
                 messageEncoding="Text"
                 textEncoding="utf-8"
                 useDefaultWebProxy="true"
                 allowCookies="false">
          <readerQuotas maxDepth="32"
                        maxStringContentLength="8192"
                        maxArrayLength="16384"
                        maxBytesPerRead="4096"
                        maxNameTableCharCount="16384"
                        />
          <reliableSession ordered="true"
                           inactivityTimeout="00:10:00"
                           enabled="false"
                           />
          <security mode="Message">
            <transport clientCredentialType="Windows"
                       proxyCredentialType="None"
                       realm=""
                       />
            <message clientCredentialType="Windows"
                     negotiateServiceCredential="true"
                     algorithmSuite="Default"
                     />
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>

    <client>
      <endpoint address="http://localhost:10000/TaskSchedulerService.svc"
                binding="wsHttpBinding"
                bindingConfiguration="wsHttpBinding"
                contract="WCFTask.ITaskServiceContract"
                name="wsHttpBinding">
      </endpoint>
    </client>
  </system.serviceModel>
</configuration>

编辑:有关范围的更多细节:

wcf 端点是在 Web 项目中定义的。 Web 项目由托管它的实例的 Windows 服务引用。 然后我有一个控制台应用程序,它引用并创建一个 Windows 服务实例以对其进行测试。

我的问题是关于让该控制台应用程序启动 Windows 服务,然后启动 WCF 端点,以便它可以在端点上进行调用,以便对我的解决方案执行一些端到端测试。

【问题讨论】:

  • 您的问题究竟是什么?您能否向我们展示您拥有的一些代码和/或配置并描述您遇到的问题/问题?
  • ok ... 1 分钟我添加一些细节
  • 如果您在 IIS 中托管,那么在您的服务器端端点元素的东西只需将地址属性值留空,因为地址由 IIS 处理,然后它应该可以正常工作
  • 我在控制台应用程序中托管 WCf 端点
  • @Wardy - 请参阅我修改后的答案。我仍然认为您将服务和客户混淆了。除非您尝试使用控制台应用程序来测试您的 Windows 服务的逻辑?

标签: c# .net wcf


【解决方案1】:

根据附加信息修改答案

好的,如果我理解正确的话,您已经创建了一个 WCF Web 应用程序,以及一个单独的 Windows 服务,该服务创建了该 WCF 服务的实例。现在您正在尝试创建一个控制台应用来测试您的解决方案。

我认为你让事情变得比他们需要的更复杂。 Windows 服务应该是调用 WCF 服务的客户端,或者它应该托管 WCF 服务本身。

无论哪种情况,如果您正在编写一个控制台应用程序来对您的解决方案进行端到端测试,那么在我看来,控制台应用程序当然是 客户端 - 正如我've 说与ServiceHost 或托管服务无关。

只需从您的控制台应用程序中启动服务(或单独启动它并使其已经运行),然后以与任何其他客户端相同的方式对 Windows 服务执行调用。

如果我仍然缺少某些东西,请告诉我,我会再试一次。我即将退出,请随时给我发电子邮件(我的地址在我的个人资料中),我明天再看。

【讨论】:

  • 那是我的问题......我已经添加了 app.config 文件并配置了服务,但它就像它忽略了配置文件,就像我在没有它的情况下这样做跨度>
  • 啊..没听懂那部分。当您在浏览器中访问该服务时,该服务是否正在运行?
  • 如果我做 ServiceHost host = new ServiceHost(typeof(HelloWorldService)) 那么浏览器找不到它但是如果我做 ServiceHost host = new ServiceHost(typeof(HelloWorldService), "localhost:10000/TaskSchedulerService.svc")) 那么可以,但它会通知我未启用元数据发布
  • 您有 .svc 页面吗?我只将它们(并且看到它们用于)用于 IIS 托管的 WCF 服务。
  • 好的,svc 是在 Web 项目中定义的,然后我有一个该项目的引用,控制台应用程序使用它来创建服务主机并启动端点(如上所示)然后添加一个项目的“服务参考”,它为我提供了生成的代理类等和配置信息以连接到服务。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多