[英]Connecting Azure Service Bus Relay (WCF endpoint) using HttpWebRequest
請幫助。 我試圖使用HttpWebRequest訪問暴露給服務總線中繼端點的WCF服務。
我通過OAuth WRAP協議從ACS成功獲得了令牌。 使用該令牌作為請求標頭中的授權,我創建了一個WebRequest以與WCF服務進行通信,該端點具有配置為WebHttpRelayBinding的終結點,以及一個用於OperationContractAttribute和WebGetAttribute的WCF服務方法。
當我運行客戶端應用程序時,出現以下錯誤:
收件人為https://namespace.servicebus.windows.net/Student/GetInfo/GetStudentInfo/1的To消息無法在接收方處理,因為EndpointDispatcher上的AddressFilter不匹配。 檢查發送方和接收方的EndpointAddresses是否一致。
我在Google上搜索,發現了將以下屬性應用於服務類的建議:
[ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)]
雖然這解決了先前的錯誤,但是現在客戶端應用程序最終出現以下錯誤:
由於EndpointDispatcher的ContractFilter不匹配,帶有Action'GET'的消息無法在接收方處理。 這可能是由於合同不匹配(發送方和接收方之間的操作不匹配)或發送方和接收方之間的綁定/安全不匹配造成的。 檢查發送方和接收方是否具有相同的合同和相同的綁定(包括安全要求,例如,消息,傳輸,無)。
我想我在WCF服務端缺少一些東西。 共享客戶代碼和服務代碼以供您查看。
WCF服務代碼:
[ServiceContract]
interface IStudentInfo
{
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Xml, UriTemplate = "/GetStudentInfo/{studentId}")]
string GetStudentInfo(string studentId);
}
[ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)]
private class StudentInfo : IStudentInfo
{
string IStudentInfo.GetStudentInfo(string studentId)
{
string returnString = null;
// .....
return returnString;
}
}
public void Run()
{
Console.WriteLine("LISTENER");
Console.WriteLine("========");
string serviceNamespace = "namespace";
string issuerName = "owner";
string issuerKey = "key";
string servicePath = "Student/GetInfo";
ServiceHost sh = new ServiceHost(typeof(StudentInfo));
// Binding
WebHttpRelayBinding binding2 = new WebHttpRelayBinding();
Uri uri = ServiceBusEnvironment.CreateServiceUri(Uri.UriSchemeHttps, serviceNamespace, servicePath);
Console.WriteLine("Service Uri: " + uri);
Console.WriteLine();
sh.AddServiceEndpoint(typeof(IStudentInfo), binding2, uri);
// Create the ServiceRegistrySettings behavior for the endpoint.
var serviceRegistrySettings = new ServiceRegistrySettings(DiscoveryType.Public);
// Create the shared secret credentials object for the endpoint matching the
// Azure access control services issuer
var sharedSecretServiceBusCredential = new TransportClientEndpointBehavior()
{
TokenProvider = TokenProvider.CreateSharedSecretTokenProvider(issuerName, issuerKey)
};
// Add the service bus credentials to all endpoints specified in configuration.
foreach (var endpoint in sh.Description.Endpoints)
{
endpoint.Behaviors.Add(serviceRegistrySettings);
endpoint.Behaviors.Add(sharedSecretServiceBusCredential);
}
sh.Open();
Console.WriteLine("Press ENTER to close");
Console.ReadLine();
sh.Close();
}
服務消費代碼:
static void Main(string[] args)
{
var studentId = "1";
string _token = GetToken();
Console.WriteLine(_token);
// Create and configure the Request
var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://namespace.servicebus.windows.net/Student/GetInfo/GetStudentInfo/" + studentId);
httpWebRequest.ContentType = "text/json";
httpWebRequest.Method = "GET";
httpWebRequest.Headers.Add(HttpRequestHeader.Authorization, string.Format("WRAP access_token=\"{0}\"", _token));
// Get the response using the Request
HttpWebResponse response = httpWebRequest.GetResponse() as HttpWebResponse;
// Read the stream from the response object
Stream stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream);
// Read the result from the stream reader
string result = reader.ReadToEnd();
Console.WriteLine("Result: " + result);
Console.ReadLine();
}
static string GetToken()
{
string base_address = string.Format("https://namespace-sb.accesscontrol.windows.net");
string wrap_name = "owner";
string wrap_password = "key";
string wrap_scope = "http://namespace.servicebus.windows.net/";
WebClient client = new WebClient();
client.BaseAddress = base_address;
NameValueCollection values = new NameValueCollection();
values.Add("wrap_name", wrap_name);
values.Add("wrap_password", wrap_password);
values.Add("wrap_scope", wrap_scope);
byte[] responseBytes = client.UploadValues("WRAPv0.9", "POST", values);
string response = Encoding.UTF8.GetString(responseBytes);
string token = response.Split('&')
.Single(value => value.StartsWith("wrap_access_token="))
.Split('=')[1];
string _token = HttpUtility.UrlDecode(token);
return _token;
}
終於完成了!
我缺少將WebHttpBehavior
服務公開為REST端點所必需的端點的WebHttpBehavior
。
endpoint.Behaviors.Add(new WebHttpBehavior());
或者,我可以在WebServiceHost
托管服務,而不是在“ ServiceHost”中托管服務,以啟用REST綁定。
WebServiceHost sh = new WebServiceHost(typeof(StudentInfo));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.