在控制台中托管简单的Wcf服务 [英] Hosting a Simple Wcf Service in Console

查看:209
本文介绍了在控制台中托管简单的Wcf服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个简单的ConsoleApplication,其中我想主持一个简单的wcf服务。



这是我的代码

 命名空间HostConsoleApplication 
{
class Program
{
static void Main(string [] args)
{
using(System.ServiceModel.ServiceHost host = new System.ServiceModel.ServiceHost(typeof(FirstWcfService.Service)))
{
host.Open();
Console.WriteLine(Sai);
Console.ReadLine();
}
}
}
}

我添加了一个app.config,看起来像这样:

 <?xml version =1.0encoding =utf- 8→> 
< configuration>
< system.serviceModel>
< services>
< service name =FirstWcfService.ServicebehaviorConfiguration =ServiceBehavior>
< endpoint address =FirstWcfServicebinding =netTcpBindingcontract =FirstWcfService.IService/>
< endpoint contract =IMetadataExchangebinding =mexHttpBindingaddress =mex/>
< host>
< baseAddresses>
< add baseAddress =net.tcp:// localhost:9101 //>
< / baseAddresses>
< / host>
< / service>
< / services>
< behaviors>
< serviceBehaviors>
< behavior name =ServiceBehavior>
< serviceMetadata httpGetEnabled =false/>
< / behavior>
< / serviceBehaviors>
< / behaviors>
< /system.serviceModel>
< / configuration>

当我运行主机控制台应用程序时,我得到这个异常:


System.InvalidOperationException是
unhandled Message =对于绑定$ b的端点,找不到与方案http
匹配的
基本地址$ b MetadataExchangeHttpBinding。
注册的基地址方案是
[net.tcp]。

Source =System.ServiceModel

StackTrace:
在System.ServiceModel.ServiceHostBase.MakeAbsoluteUri(Uri
relativeOrAbsoluteUri,Binding
binding,UriSchemeKeyedCollection
baseAddresses)
在System.ServiceModel.Description.ConfigLoader.LoadServiceDescription(ServiceHostBase
主机,ServiceDescription描述,
ServiceElement serviceElement,
Action`1 addBaseAddress)
在System.ServiceModel.ServiceHostBase.LoadConfigurationSectionInternal(ConfigLoader
configLoader,ServiceDescription
description,ServiceElement
serviceSection)
在System.ServiceModel.ServiceHostBase.LoadConfigurationSectionInternal(ConfigLoader
configLoader,ServiceDescription
description,String configurationName)
在System.ServiceModel.ServiceHostBase.ApplyConfiguration()
在System.ServiceModel.ServiceHostBase.InitializeDescription(UriSchemeKeyedCollection
baseAddresses)
在System.ServiceModel.ServiceHost.InitializeDescription(类型
serviceType,UriSchemeKeyedCollection
baseAddresses)
在System.ServiceModel.ServiceHost..ctor(类型
serviceType,Uri [] baseAddresses)
在HostConsoleApplication.Program.Main(String []
args)在C:\Documents和
Settings\\\
avin.pathuru\My
Documents \Visual Studio
2008\Projects\Solution2\HostConsoleApplication\Program.cs:line
13
at System.AppDomain._nExecuteAssembly(Assembly
assembly,String [] args)
在System.AppDomain.ExecuteAssembly(String
assemblyFile,Evidence
assemblySecurity,String [] args )
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
在System.Threading.ThreadHelper.ThreadStart_Context(对象
状态)
在System.Threading.ExecutionContext.Run ExecutionContext
executionContext,ContextCallback
callback,Object state)
在System.Threading.ThreadHelper.ThreadStart()
InnerException:


只是想知道如何解决这个问题。
感谢
N

解决方案

好吧,我想问题是这样:




  • 您有net.tcp

  • 的基地址定义了一个MEX http端点(但没有http基地址)



基本上,如果您想通过http使用MEX,则需要为 MEX端点或http基地址(如果只指定相对地址)。



解决方案1:指定MEX的完整地址端点:

 < services> 
< service name =FirstWcfService.Service
behaviorConfiguration =ServiceBehavior>
< endpoint
address =FirstWcfService
binding =netTcpBinding
contract =FirstWcfService.IService/>
< endpoint
address =http:// localhost:9102 / FirstWcfService / mex
binding =mexHttpBinding
contract =IMetadataExchange/&
......
< / service>
< / services>解决方案2:也定义一个HTTP基地址:

 < services> 
< service name =FirstWcfService.Service
behaviorConfiguration =ServiceBehavior>
< endpoint
address =FirstWcfService
binding =netTcpBinding
contract =FirstWcfService.IService/>
< endpoint
address =mex
binding =mexHttpBinding
contract =IMetadataExchange/&
< host>
< baseAddresses>
< add baseAddress =net.tcp:// localhost:9101 //>
< add baseAddress =http:// localhost:9102 //>
< / baseAddresses>
< / host>
< / service>
< / services>解决方案3:使用mexTcpBinding

$ b $ 。 b

 < services> 
< service name =FirstWcfService.Service
behaviorConfiguration =ServiceBehavior>
< endpoint
address =FirstWcfService
binding =netTcpBinding
contract =FirstWcfService.IService/>
< endpoint
address =mex
binding =mexTcpBinding
contract =IMetadataExchange/>
......
< / service>
< / services>

这三个选项中的任何一个都应该可以解决。



谨慎的一句话:我发现调用你的服务行为配置ServiceBehavior是很危险的......

 < serviceBehaviors> 
< behavior name =ServiceBehavior>

我的建议:调用你的第一个和默认配置只是普通的默认(或DefaultBehavior / p>

 < serviceBehaviors> 
< behavior name =Default>

,如果您有多个配置,只有开始给出其他名称。



调用这个 ServiceBehavior 似乎在一段时间后要求麻烦.....


I am trying to create a simple ConsoleApplication in which i would like to host a simple wcf service.

Here is the code for my

namespace HostConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            using (System.ServiceModel.ServiceHost host = new System.ServiceModel.ServiceHost(typeof(FirstWcfService.Service)))
            {
                host.Open();
                Console.WriteLine("Sai");
                Console.ReadLine();
            }
        }
    }
}

Then i have added an app.config which looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <services>
            <service name="FirstWcfService.Service" behaviorConfiguration="ServiceBehavior">
                <endpoint address="FirstWcfService" binding="netTcpBinding" contract="FirstWcfService.IService"/>
                <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
                <host>
                    <baseAddresses>
                        <add baseAddress="net.tcp://localhost:9101/"/>
                    </baseAddresses>
                </host>
            </service>
        </services>
        <behaviors>
            <serviceBehaviors>
                <behavior name="ServiceBehavior" >
                    <serviceMetadata httpGetEnabled="false" />
                </behavior>
            </serviceBehaviors>
        </behaviors>
    </system.serviceModel>
</configuration>

When i run the host console app i get this exception:

System.InvalidOperationException was unhandled Message="Could not find a base address that matches scheme http for the endpoint with binding MetadataExchangeHttpBinding. Registered base address schemes are [net.tcp]."
Source="System.ServiceModel"
StackTrace: at System.ServiceModel.ServiceHostBase.MakeAbsoluteUri(Uri relativeOrAbsoluteUri, Binding binding, UriSchemeKeyedCollection baseAddresses) at System.ServiceModel.Description.ConfigLoader.LoadServiceDescription(ServiceHostBase host, ServiceDescription description, ServiceElement serviceElement, Action`1 addBaseAddress) at System.ServiceModel.ServiceHostBase.LoadConfigurationSectionInternal(ConfigLoader configLoader, ServiceDescription description, ServiceElement serviceSection) at System.ServiceModel.ServiceHostBase.LoadConfigurationSectionInternal(ConfigLoader configLoader, ServiceDescription description, String configurationName) at System.ServiceModel.ServiceHostBase.ApplyConfiguration() at System.ServiceModel.ServiceHostBase.InitializeDescription(UriSchemeKeyedCollection baseAddresses) at System.ServiceModel.ServiceHost.InitializeDescription(Type serviceType, UriSchemeKeyedCollection baseAddresses) at System.ServiceModel.ServiceHost..ctor(Type serviceType, Uri[] baseAddresses) at HostConsoleApplication.Program.Main(String[] args) in C:\Documents and Settings\navin.pathuru\My Documents\Visual Studio 2008\Projects\Solution2\HostConsoleApplication\Program.cs:line 13 at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() InnerException:

Just wondering if how to fix this. Thanks N

解决方案

Well, I think the problem is this:

  • you have a base address for net.tcp
  • you have a MEX http endpoint defined (but no http base address)

Basically if you want to use MEX over http, you need to supply either a full address for the MEX endpoint, or a http base address (if you only specify a relative address).

Solution 1: specify a full address for the MEX endpoint:

 <services>
    <service name="FirstWcfService.Service" 
              behaviorConfiguration="ServiceBehavior">
       <endpoint 
           address="FirstWcfService" 
           binding="netTcpBinding" 
           contract="FirstWcfService.IService"/>
       <endpoint 
           address="http://localhost:9102/FirstWcfService/mex"
           binding="mexHttpBinding" 
           contract="IMetadataExchange"  />
        ......
    </service>
</services>

Solution 2: define an HTTP base address, too:

 <services>
    <service name="FirstWcfService.Service" 
              behaviorConfiguration="ServiceBehavior">
       <endpoint 
           address="FirstWcfService" 
           binding="netTcpBinding" 
           contract="FirstWcfService.IService"/>
       <endpoint 
           address="mex"
           binding="mexHttpBinding" 
           contract="IMetadataExchange"  />
       <host>
           <baseAddresses>
               <add baseAddress="net.tcp://localhost:9101/"/>
               <add baseAddress="http://localhost:9102/"/>
           </baseAddresses>
       </host>
    </service>
</services>

Solution 3: use the mexTcpBinding instead

 <services>
    <service name="FirstWcfService.Service" 
              behaviorConfiguration="ServiceBehavior">
       <endpoint 
           address="FirstWcfService" 
           binding="netTcpBinding" 
           contract="FirstWcfService.IService"/>
       <endpoint 
           address="mex"
           binding="mexTcpBinding" 
           contract="IMetadataExchange"  />
        ......
    </service>
</services>

Any of those three options should should solve it.

A word of caution: I find it quite risky to call your service behavior configuration "ServiceBehavior"......

<serviceBehaviors>
    <behavior name="ServiceBehavior" >

My recommendation: call your first and default configuation just plain "Default" (or "DefaultBehavior")

<serviceBehaviors>
    <behavior name="Default" >

and only start giving out other names if you have multiple configurations.

Calling this ServiceBehavior just seems to be asking for trouble some time later on.....

这篇关于在控制台中托管简单的Wcf服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆