WCF 服务(非 Web)配置警告
WCF Service (non-web) Configuration Warning
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.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.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"/>
我已经知道 contract
是 namespace.interface
。
如果我将服务和接口文件放在子目录 Communication 中,我没有,但说我做了,那么
<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.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.0\App\MyServiceLibrary\App.config 11
有几个 SO 主题,通常是关于 Web 服务的,但是 this one 具有代表性并且说的是我刚刚所做的。
我错过了什么?
最重要的是,因为你说这是一个 非网络 配置,这意味着你是 自托管 ...意味着有一个 .exe 项目的小垫片启动(windows 服务或自动激活或其他)。 .exe 项目的 app.config 需要所有这些配置信息。
我建议这样做的原因是因为它一直在说 MyServiceLibrary.MyService
和 MyServiceLibrary.IMyService
,这意味着您所做的编辑不会在您的应用程序启动时反映出来...这意味着(可能)您正在编辑库项目(.dll)中的 app.config。那不行。
(注意评论中的警告:
<!-- 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. -->
当我第一次偶然发现这个特定问题时,他们没有收到警告。但是,我了解配置文件中的注释......它们只是噪音太大以至于它们完全消失了;-)
一旦您编辑了正确的 .config,您将开始收到更好的警告,总的来说您似乎走在了正确的轨道上。
因此,如果您有一些启动该服务的 .exe 项目,它必须创建您的服务实例。
svc = new ServiceHost( typeof( ServiceNamespace.ServiceClass ) );
svc.Open();
...其中 svc
是某种持久对象(取决于主机)。如果它是一个 Windows 服务,您的 WCF 服务将进入您覆盖的 ServiceBase
class 的实例数据......并且上述代码进入 OnStart
方法。如果它是控制台应用程序,您可以创建一个 Main
方法来创建对象,然后在循环中休眠(打开的 wcf 服务不会在启动线程中侦听)。
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.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.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"/>
我已经知道 contract
是 namespace.interface
。
如果我将服务和接口文件放在子目录 Communication 中,我没有,但说我做了,那么
<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.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.0\App\MyServiceLibrary\App.config 11
有几个 SO 主题,通常是关于 Web 服务的,但是 this one 具有代表性并且说的是我刚刚所做的。
我错过了什么?
最重要的是,因为你说这是一个 非网络 配置,这意味着你是 自托管 ...意味着有一个 .exe 项目的小垫片启动(windows 服务或自动激活或其他)。 .exe 项目的 app.config 需要所有这些配置信息。
我建议这样做的原因是因为它一直在说 MyServiceLibrary.MyService
和 MyServiceLibrary.IMyService
,这意味着您所做的编辑不会在您的应用程序启动时反映出来...这意味着(可能)您正在编辑库项目(.dll)中的 app.config。那不行。
(注意评论中的警告:
<!-- 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. -->
当我第一次偶然发现这个特定问题时,他们没有收到警告。但是,我了解配置文件中的注释......它们只是噪音太大以至于它们完全消失了;-)
一旦您编辑了正确的 .config,您将开始收到更好的警告,总的来说您似乎走在了正确的轨道上。
因此,如果您有一些启动该服务的 .exe 项目,它必须创建您的服务实例。
svc = new ServiceHost( typeof( ServiceNamespace.ServiceClass ) );
svc.Open();
...其中 svc
是某种持久对象(取决于主机)。如果它是一个 Windows 服务,您的 WCF 服务将进入您覆盖的 ServiceBase
class 的实例数据......并且上述代码进入 OnStart
方法。如果它是控制台应用程序,您可以创建一个 Main
方法来创建对象,然后在循环中休眠(打开的 wcf 服务不会在启动线程中侦听)。