WCF服务(非Web)配置警告

问题描述 投票:2回答:1

Visual Studio不断向我发出2条警告:

Severity    Code    Description Project File    Line    Suppression State
Warning     WCF configuration validation warning: The 'name' attribute is invalid - The value 'MyServiceLibrary.MyService' is invalid according to its datatype 'serviceNameType'.  MyServiceLibrary    C:\MyDrive\Flavor\Net\1.0\App\MyServiceLibrary\App.config   10  
Warning     WCF configuration validation warning: The 'contract' attribute is invalid - The value 'MyServiceLibrary.IMyService' is invalid according to its datatype 'serviceContractType'. MyServiceLibrary    C:\MyDrive\Flavor\Net\1.0\App\MyServiceLibrary\App.config   11  

这是app.config文件的一部分:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <!-- When deploying the service library project, the content of the config file must be added to the host's 
  app.config file. System.Configuration does not support config files for libraries. -->
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2"/>
    </startup>
    <system.serviceModel>
        <services>
            <service behaviorConfiguration="MyServiceBehavior" name="MyIpc.IpcAppToService">
                <endpoint address="http://localhost:8733/MyIpcAppToService" binding="basicHttpBinding" bindingConfiguration="MyAppToIPCEndpointBinding" name="MyIpc_IIpcAppToService" contract="MyIpc.IIpcAppToService"/>
                <endpoint address="mex" binding="mexHttpBinding" name="mex" contract="IMetadataExchange"/>
                <host>
                    <baseAddresses>
                        <add baseAddress="http://localhost:8733/MyService/"/>
                    </baseAddresses>
                </host>
            </service>
        </services>
        ...
    </system.serviceModel>
</configuration>

这是服务文件:

namespace MyServiceLibrary
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    public class MyService : IMyService
    {
        ...
    }
}

这是接口文件:

using System.ServiceModel;
using System.ServiceModel.Web;

namespace MyServiceLibrary
{
    [ServiceContract]
    public interface IMyService
    {
        [WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, UriTemplate = "/GetVersionInfo")]
        [OperationContract]
        string GetVersionInfo();
    }
}

这是我的理解(app.config):

<service behaviorConfiguration="namespace" name="<namespace>.<service name>">

<endpoint address="..." ... contract="<namespace>.<interface name>"/>

在我的情况下,服务名称不驻留在子目录中,因此:

namespace=MyServiceLibrary
service name=MyService    (file is MyService.cs)
interface=IMyService      (file is IMyService.cs)

这使得:

<service ... name="MyServiceLibrary.MyService">

 <endpoint ... contract="MyServiceLibrary.IMyService"/>

我已经知道contractnamespace.interface的事实。

如果我把服务和接口文件放在子目录,通讯,我没有,但说我做了,然后

<service ... name="MyServiceLibrary.Communication.MyService">
<endpoint ... contract="MyServiceLibrary.Communication.IMyService"/>

其中MyServiceLibrary.Communication是命名空间。

令人讨厌的是,我仍然收到警告:

 Severity   Code    Description Project File    Line    Suppression State
 Warning        WCF configuration validation warning: The 'name' attribute is invalid - The value 'MyServiceLibrary.MyService' is invalid according to its datatype 'serviceNameType'.  MyServiceLibrary    C:\MyDrive\Flavor\Net\1.0\App\MyServiceLibrary\App.config   10  
 Warning        WCF configuration validation warning: The 'contract' attribute is invalid - The value 'MyServiceLibrary.IMyService' is invalid according to its datatype 'serviceContractType'. MyServiceLibrary    C:\MyDrive\Flavor\Net\1.0\App\MyServiceLibrary\App.config   11  

有几个SO主题,通常在Web服务上,但this one是代表性的,并说我刚刚做了什么。

我错过了什么?

c# wcf app-config
1个回答
2
投票

最重要的是,因为你说这是一个非web配置,这意味着你是自托管的...意味着有一个.exe项目的小垫片启动(Windows服务或自动激活或其他东西) 。这是.exe项目的app.config,需要所有这些配置信息。

我之所以提出这个原因,是因为它一直在说MyServiceLibrary.MyServiceMyServiceLibrary.IMyService意味着当你的应用程序启动时,你所做的编辑没有被反映...意思(可能)你正在编辑库项目中的app.config( .DLL)。这不起作用。

(注意评论中的警告:

<! - 部署服务库项目时,必须将配置文件的内容添加到主机的app.config文件中。 System.Configuration不支持库的配置文件。 - >

当我第一次偶然发现这个问题时,他们没有发出警告。但是,我理解配置文件中的注释......它们只是噪音很大,它们完全消失了;-)

一旦你正在编辑正确的.config,你就会开始获得更好的警告,而且大体上你似乎走在了正确的轨道上。

所以,如果你有一个启动服务的.exe项目,它必须创建你的服务实例。

svc = new ServiceHost( typeof( ServiceNamespace.ServiceClass ) );
svc.Open();

...其中svc是某种持久性对象(取决于主机)。如果它是Windows服务,那么您的WCF服务将进入被覆盖的ServiceBase类的实例数据......并且上述代码将进入OnStart方法。如果它是一个控制台应用程序,你创建一个Main方法创建对象,然后只是在一个循环中休眠(打开wcf服务不会在启动线程中监听)。

© www.soinside.com 2019 - 2024. All rights reserved.