我在 IIS 上的 Windows 2019 服務器上運行了 WCF 服務。該服務將由在 xamarin 環境中使用的 .Net Core 庫使用。一切正常,直到我想添加我的自定義 UserNamePasswordValidator 以便為服務添加一些安全性。
我的服務器的 Web.config 服務部分如下所示
<system.serviceModel>
<services>
<service name="aircu_web.UpdateService" behaviorConfiguration="debug">
<endpoint address="" name="wsHttpEndpoint" binding="wsHttpBinding" bindingConfiguration="ServiceBinding" contract="aircu_web.IUpdateService" />
</service>
</services>
<bindings>
<wsHttpBinding>
<binding name="ServiceBinding">
<security mode ="Message">
<message clientCredentialType="UserName"/>
</security>
</binding>
</wsHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="production">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
<behavior name="debug">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
<serviceCredentials>
<serviceCertificate storeLocation="LocalMachine" x509FindType="FindByThumbprint" findValue="84 d2 c6 d8 85 17 48 30 04 9e 12 e0 bd f4 f3 36 c1 57 b5 6e" storeName="My"/>
<userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="aircu_web.ApiKeyValidator,aircu_web"/>
</serviceCredentials>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
導航到 https://(mydomain)/UpdateService.svc 時,我可以看到“您已創建服務”站點。
當我向我的程式集添加服務參考時,向導作業正常,我得到了所有生成的代碼,除了系結和端點地址——這對我來說是新的。在更改服務之前,我的服務客戶端的建構式不需要額外的引數。
當我使用以下代碼創建客戶端并呼叫 api
var binding = new BasicHttpsBinding();
var ep = new EndpointAddress(@"https://(my domain)/UpdateService.svc");
var svc = new AirCU.Service.UpdateServiceClient(binding, ep);
我得到例外在 https://(my domain)/UpdateService.svc 上沒有可以接受訊息的端點偵聽。這通常是由不正確的地址或 SOAP 操作引起的。有關更多詳細資訊,請參閱 InnerException(如果存在)。”
我猜我的配置中存在問題,無論是客戶端還是服務器......
為了讓一切恢復正常,我需要進行哪些更改?:)
非常感謝
uj5u.com熱心網友回復:
好吧,我自己想通了。
Xamarin 和 .Net Standard 不支持 wsHttpEndpoints。我不得不切換到 basicHttpsBinding,Xamarin 不支持安全模式 TransportWithMessageCredential,所以我只能堅持使用 Transport。
最后,我的 Web.config 部分如下所示:
<system.serviceModel>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<services>
<service name="aircu_web.UpdateService" behaviorConfiguration="debug">
<endpoint address="" name="HttpsEndpoint" binding="basicHttpsBinding" bindingConfiguration="ServiceBinding" contract="aircu_web.IUpdateService" />
</service>
</services>
<bindings>
<basicHttpsBinding>
<binding name="ServiceBinding" maxReceivedMessageSize="1000000">
<security mode ="Transport">
<message clientCredentialType="UserName"/>
</security>
</binding>
</basicHttpsBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="production">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
<behavior name="debug">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
<serviceCredentials>
<serviceCertificate storeLocation="LocalMachine" x509FindType="FindByThumbprint" findValue="84 d2 c6 d8 85 17 48 30 04 9e 12 e0 bd f4 f3 36 c1 57 b5 6e" storeName="My"/>
<userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="aircu_web.ApiKeyValidator,aircu_web"/>
</serviceCredentials>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
我還必須相應地更改我的客戶端代碼。
private UpdateService.UpdateServiceClient GetProxy()
{
var binding = new BasicHttpsBinding();
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
binding.Security.Mode = BasicHttpsSecurityMode.Transport;
var ep = new EndpointAddress(@"https://(mydomain)/UpdateService.svc");
var svc = new UpdateService.UpdateServiceClient(binding, ep);
svc.ClientCredentials.UserName.UserName = "App";
svc.ClientCredentials.UserName.Password = "****";
return svc;
}
我還不得不將我的服務參考從我的庫專案移動到我的 xamarin 專案,因為我無法弄清楚 System.ServiceModel 庫的一些程式集不匹配。
我嘗試將相同的 nuget 包安裝到所有專案,洗掉它們,清除 bin 和 obj 檔案夾,重新創建專案 - 沒有任何效果。所以我將整個服務參考移至 xamarin 專案,現在它作業正常。也許有人可以告訴我那里出了什么問題。錯誤是“型別(某些數字)未知......”
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/475745.html
