c# - WCF 服务(非 Web)配置警告

标签 c# wcf app-config

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
    {
        ...
    }
}

这是接口(interface)文件:

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

如果我将服务和接口(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具有代表性并说出了我刚刚所做的事情。

我错过了什么?

最佳答案

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

我建议这样做的原因是因为它一直在说 MyServiceLibrary.MyServiceMyServiceLibrary.IMyService 这意味着您所做的编辑不会在您的应用程序启动时反射(reflect)出来...意味着(可能)您正在编辑库项目(.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 类的实例数据...并且上述代码进入 OnStart 方法。如果它是控制台应用程序,则创建一个创建对象的 Main 方法,然后在循环中休眠(打开的 wcf 服务不在启动线程中监听)。

关于c# - WCF 服务(非 Web)配置警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55365141/

相关文章:

c# - 在 DefaultControllerFactory 中使用 Unity 2 解决命名注册

c# - 从选择选项标签中检索文本(不是值)

asp.net - "maximum string content length quota (8192) has been exceeded while reading XML data"通过 mexAddress 名称调用 WCF

wcf - maxReceivedMessageSize 未修复 413 : Request Entity Too Large

app-config - 自定义配置属性 - 条目已添加

c# - 无法使用 CommandLineParser 包运行程序

c# - 传递 List<MyType> 时,使用 Param List<T> 的 WCF 调用失败

c# - 用 AfterBuild 替换 <appname>.exe.config 不适用于 <appname>.vshost.exe.config

c# - 如何允许用户编辑 Program Files 下的 App.config 是记事本或其他编辑器?

c# - 使用 JavaScriptSerializer 格式化 JSON