幫助請。我試圖使用HttpWebRequest訪問暴露給服務總線中繼端點的WCF服務。使用HttpWebRequest連接Azure服務總線中繼(WCF端點)
我成功地通過OAuth WRAP協議從ACS獲得令牌。使用該令牌作爲請求頭中的授權,我創建了一個WebRequest來與WCF服務進行通信,其中一個端點配置爲WebHttpRelayBinding,另一個WCF服務方法與OperationContractAttribute和WebGetAttribute一起應用。
當我運行客戶端應用程序,我得到了以下錯誤:
The message with To ' https://namespace.servicebus.windows.net/Student/GetInfo/GetStudentInfo/1 ' cannot be processed at the receiver, due to an AddressFilter mismatch at the EndpointDispatcher. Check that the sender and receiver's EndpointAddresses agree.
我用Google搜索,發現了一個建議,以下屬性適用於服務類:
[ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)]
雖然這解決了以前的錯誤,但是現在客戶端應用程序以下列錯誤結束:
The message with Action 'GET' cannot be processed at the receiver, due to a ContractFilter mismatch at the EndpointDispatcher. This may be because of either a contract mismatch (mismatched Actions between sender and receiver) or a binding/security mismatch between the sender and the receiver. Check that sender and receiver have the same contract and the same binding (including security requirements, e.g. Message, Transport, None).
我想我在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;
}