查詢CRM Online時,如何配置ASP.NET Web表單以通過wsHttpBinding
而不是basicHttpBinding
發送請求?如何在查詢CRM Online時將ASP.NET網頁配置爲使用wsHttpBinding?
作爲一個背景,這是一個在IIS 8.5本地託管的非常基本的ASPX頁面。
該頁面旨在連接CRM Online並提取一些數據。
編輯:此應用程序通過Azure AD進行身份驗證,因此它使用客戶端ID和密鑰。我想回避使用連接字符串和域憑證,除非這是不可避免的。
我想我已經得到了驗證,CRM正確的,但是當我運行一個查詢我收到以下錯誤:
System.Net.WebException: The remote server returned an error: (415) Cannot process the message because the content type 'text/xml; charset=utf-8' was not the expected type 'application/soap+xml; charset=utf-8'..
我想這是因爲我沒有指定在web.config
一個綁定,我的頁面通過basicHttpBinding
發送所有內容,CRM Online界面預計爲wsHttpBinding
。
我試過使用this MSDN article(及其變體)底部的例子,但它沒有區別。
以下是連接CRM的代碼,以便它有所作爲。顯然我已經模糊了真實的配置細節。
// CONFIGURE OAUTH
var tenantName = "mycompany.onmicrosoft.com";
var authString = string.Format(@"https://login.microsoftonline.com/{0}",tenantName);
var authContext = new AuthenticationContext(authString, false);
var clientId = "123ab123-123a-1a23-abcd-1a2345612345";
var key = "nStgfrdk0oyaC1P5+/FQ4wGn4fRgUTr8HTKejytf0bv=";
var clientCred = new ClientCredential(clientId, key);
var resource = "https://myinstance.crm4.dynamics.com";
// ACQUIRE THE AUTH TOKEN
var authResult = await authContext.AcquireTokenAsync(resource, clientCred);
// CREATE THE CONNECTION TO CRM
var orgService = new OrganizationWebProxyClient(
new Uri("https://dev.crm4.dynamics.com/XRMServices/2011/Discovery.svc"), true)
{
HeaderToken = authResult.AccessToken,
SdkClientVersion = "8.1.0"
};
// RUNNING THIS QUERY CAUSES THE ERROR
var contacts = orgService.RetrieveMultiple(new QueryExpression
{
EntityName = "contact",
ColumnSet = new ColumnSet("firstname", "lastname")
})
.Entities
.Select(item => item.ToEntity<Contact>());
下面是情況下,堆棧跟蹤它的使用範圍:
[WebException: The remote server returned an error: (415) Cannot process the message because the content type 'text/xml; charset=utf-8' was not the expected type 'application/soap+xml; charset=utf-8'..] System.Net.HttpWebRequest.GetResponse() +1740 System.ServiceModel.Channels.HttpChannelRequest.WaitForReply(TimeSpan timeout) +75
[ProtocolException: Content Type text/xml; charset=utf-8 was not supported by service https://dev.crm4.dynamics.com/XRMServices/2011/Discovery.svc . The client and service bindings may be mismatched.] System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) +14350190 System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type) +388 Microsoft.Xrm.Sdk.IOrganizationService.RetrieveMultiple(QueryBase query) +0 Microsoft.Xrm.Sdk.WebServiceClient.WebProxyClient
1.ExecuteAction(Func
1 action) +51 Quote_Robot_Demo.d__4.MoveNext() +887 System.Runtime.CompilerServices.<>c.b__6_0(Object state) +56 System.Web.Util.SynchronizationHelper.SafeWrapCallback(Action action) +110 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +13847892 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +61 System.Web.Util.WithinCancellableCallbackTaskAwaiter.GetResult() +32 System.Web.UI.d__523.MoveNext() +7970
編輯:一旦我添加服務參考我在web.config中得到這個:
<system.serviceModel>
<bindings>
<customBinding>
<binding name="CustomBinding_IDiscoveryService">
<textMessageEncoding />
<httpsTransport />
</binding>
</customBinding>
</bindings>
<client>
<endpoint address="https://dev.crm4.dynamics.com/XRMServices/2011/Discovery.svc"
binding="customBinding" bindingConfiguration="CustomBinding_IDiscoveryService"
contract="CRMService.IDiscoveryService" name="CustomBinding_IDiscoveryService" />
</client>
</system.serviceModel>
爲什麼要重新發明輪子? https://msdn.microsoft.com/zh-cn/library/gg695810(v=crm.7).aspx – dynamicallyCRM
我應該指定此應用程序通過Azure AD進行身份驗證,因此它使用唯一的客戶端ID和密鑰Azure登錄。SDK示例使用連接字符串,但它們需要Windows憑據,我不想這樣做。我會編輯我的問題,以更具體地瞭解這一點。 – Equalsk
CRM SDK不僅適用於本地安裝,它還支持CRM在線版本,因此無論託管應用程序的方式如何,您都可以使用SDK連接到CRM實例,無論託管方式如何。此外,後臺的SDK完全符合您的要求,它爲您處理所有溝通渠道。 – dynamicallyCRM