为了账号安全,请及时绑定邮箱和手机立即绑定

WCF 服务(非 Web)配置警告

WCF 服务(非 Web)配置警告

C#
江户川乱折腾 2022-11-13 17:26:51
Visual Studio 不断向我发出 2 条警告:Severity    Code    Description Project File    Line    Suppression StateWarning     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    {        ...    }}我错过了什么?
查看完整描述

1 回答

?
炎炎设计

TA贡献1808条经验 获得超4个赞

最重要的是,因为您说这是一个非 Web配置,这意味着您是自托管的……这意味着启动了一个 .exe 项目的小垫片(Windows 服务或自动激活等) . 正是那个 .exe 项目的app.config需要所有这些配置信息。

我建议这样做的原因是因为它一直在说MyServiceLibrary.MyService并且MyServiceLibrary.IMyService意味着你所做的编辑在你的应用程序启动时没有得到反映......意思是(可能)你正在编辑库项目中的app.config(.dll ). 那是行不通的。

(注意评论中的警告:

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

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

一旦你编辑了正确的.config,你就会开始收到更好的警告,而且总的来说你似乎走在了正确的轨道上。

因此,如果您有一些启动服务的 .exe 项目,它必须创建您的服务的实例。

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

...哪里svc是某种持久对象(取决于主机)。如果它是 Windows 服务,则 WCF 服务进入重写ServiceBase类的实例数据……并且上述代码进入OnStart方法。如果它是一个控制台应用程序,您可以创建一个Main创建对象的方法,然后只是在一个循环中休眠(打开的 wcf 服务不会在启动线程中侦听)。


查看完整回答
反对 回复 2022-11-13
  • 1 回答
  • 0 关注
  • 63 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信