[英]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\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 服務的,但這個主題具有代表性,並說明了我剛剛所做的。
我錯過了什么?
最重要的是,因為你說這是一個非網絡配置,這意味着你是自托管的......意味着有一個 .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 服務不會在啟動線程中偵聽)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.