【发布时间】:2019-08-17 07:23:47
【问题描述】:
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"/>
我已经知道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\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 具有代表性,说明了我刚刚所做的。
我错过了什么?
【问题讨论】:
-
VisualStudio 甚至为 .dll 项目制作 app.config,这让情况更加恶化。
标签: c# wcf app-config