在控制台中托管简单的Wcf服务

Hosting a Simple Wcf Service in Console

我正在尝试创建一个简单的控制台应用程序,在该应用程序中我希望托管一个简单的WCF服务。

这是我的密码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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();
            }
        }
    }
}

然后我添加了一个app.config,如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?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>
                       
                    </baseAddresses>
                </host>
            </service>
        </services>
        <behaviors>
            <serviceBehaviors>
                <behavior name="ServiceBehavior">
                    <serviceMetadata httpGetEnabled="false" />
                </behavior>
            </serviceBehaviors>
        </behaviors>
    </system.serviceModel>
</configuration>

当我运行主机控制台应用程序时,会收到以下异常:

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
avin.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:

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


嗯,我认为问题是:

  • 您有net.tcp的基址
  • 您定义了一个MEX HTTP端点(但没有HTTP基地址)

基本上,如果要在HTTP上使用MEX,则需要为MEX端点提供完整地址,或者提供HTTP基地址(如果只指定相对地址)。

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 <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基地址:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 <services>
    <service name="FirstWcfService.Service"
              behaviorConfiguration="ServiceBehavior">
       <endpoint
           address="FirstWcfService"
           binding="netTcpBinding"
           contract="FirstWcfService.IService"/>
       <endpoint
           address="mex"
           binding="mexHttpBinding"
           contract="IMetadataExchange"  />
       <host>
           <baseAddresses>
               
               
           </baseAddresses>
       </host>
    </service>
</services>

解决方案3:改用mextcpbinding

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 <services>
    <service name="FirstWcfService.Service"
              behaviorConfiguration="ServiceBehavior">
       <endpoint
           address="FirstWcfService"
           binding="netTcpBinding"
           contract="FirstWcfService.IService"/>
       <endpoint
           address="mex"
           binding="mexTcpBinding"
           contract="IMetadataExchange"  />
        ......
    </service>
</services>

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

请注意:我发现将您的服务行为配置称为"服务行为"非常危险……

1
2
<serviceBehaviors>
    <behavior name="ServiceBehavior">

我的建议是:将您的第一个和默认配置简单地称为"默认"(或"默认行为")。

1
2
<serviceBehaviors>
    <behavior name="Default">

只有在有多个配置的情况下才开始给出其他名称。

称之为ServiceBehavior,似乎只是在一段时间后要求麻烦……