WCF + Azure = Nightmare!
I've s开发者_如何转开发pent the prior week trying to get a secure form of WCF to work on Azure, but all to no avail! My use case is pretty simple. I want to call a WCF endpoint in the cloud and pass messages to be queued for a Worker Role. Beyond that I want to limit access to pre-authrorized users, authenticated via username & password.
I've tried to get this working with Transport, TransportWithMessageCredential and Message security but nothing seems to work. Indeed, I've worked through every example and snippet that I could find, most recently the "Service using binary HTTP binding with transport security and message credentials and Silverlight client" example on the http://code.msdn.microsoft.com/wcfazure page. I'm pretty sure that I'm being knocked down by small bugs and beta changes but the end result is that I'm totally stuck.
This is a critical path item for me so any suggestions would be greatly appreciated. A complete working example or a walkthrough would be even better!
Can't answer your question per se - but have you checked out:
- Windows Azure + WCF samples
- Hosting WCF services in Azure
- Building distributed applications with .NET Services
- Windows Azure Whitepapers by Pluralsight
I encountered a lot of issues protecting my web service via user name and password as well. I finally choose Message security via Certificate it seemed to be easier to implement. Step by step message security WCF
I only know how to do this with anonymous users, but perhaps my setup will get you help you to figure it out. The code below I know work with anonymous users, perhaps you can fiddle with it to get it working with authentication. Also, the only way to secure a Web endpoint is to expose it through HTTPS, using transport security ( source: http://msdn.microsoft.com/en-us/library/aa702697.aspx ).
web.config
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" />
</system.serviceModel>
Inferface
IExample.cs:
using System.ServiceModel;
using System.ServiceModel.Web;
namespace WebPages.Interfaces
{
[ServiceContract]
public interface IExample
{
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json)]
string GetSomething(string id);
}
}
ExampleService.svc.cs markup
<%@ ServiceHost Language="C#" Debug="true" Service="WebPages.Interfaces.ExampleService" CodeBehind="ExampleService.svc.cs" Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>
ExampleService.svc.cs codebehind
namespace WebPages.Interfaces
{
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class ExampleService : IExample
{
string JsonSerializeSomething(Something something)
{
var serializer = new DataContractJsonSerializer(something.GetType());
var memoryStream = new MemoryStream();
serializer.WriteObject(memoryStream, something);
return Encoding.Default.GetString(memoryStream.ToArray());
}
public string GetSomething(string id)
{
var something = DoSomeBusinessLogic(id);
return JsonSerializeSomething(something);
}
}
}
jQuery call from client
function _callServiceInterface(id, delegate) {
var restApiCall = "Interfaces/ExampleService.svc/GetSomething?id="
+ escape(id);
$.getJSON(restApiCall, delegate);
}
function _getSomethingFromService() {
_callServiceInterface('123',
function (result) {
var parsedResult = $.parseJSON(result);
$('#info').html(result.SomethingReturnedFromServiceCall);
}
);
}
Old question and Im not entirely sure where your solution failed but from what I understand you need a digital certificate from a recognised trusted source, not just a privately created certificate to make WCF work with Azure.
If you dont use one then as I understand it the WCF will fail but not really say why. So you can end up chasing the ghost of a code bug that's not actually there.
精彩评论