我正試圖在一個輔助角色的Azure上承載一個面向外部的WCF服務。Azure工作角色中的外部HTTP端點可能嗎?
我有一個在本地非常好的解決方案,但是當我嘗試將它發佈到Azure時,它會進入初始化/忙/停止循環。
我在互聯網找到的信息說,不同的事情:
http://www.theworkflowelement.com/2011/01/worker-role-service-hosting-faq.html(不可能)
http://code.msdn.microsoft.com/WCF-Azure-Worker-Role-on-b394df49(可能的黑客)
其他消息來源說,這是可能的,但我不有代表張貼超過兩個鏈接。
當我嘗試發佈它時,最後一個在忙。
任何人都知道如何做到這一點,或者如果它真的不可能?將它作爲工作角色託管非常好,所以我不必使用Web角色所需的svc和web.config混亂。
這是我使用的代碼:
[ServiceContract(Namespace = "")]
public interface IMyService
{
[OperationContract]
[WebGet]
string Echo(string s);
}
public class MyService : IMyService
{
public string Echo(string s)
{
return "hey " + s;
}
}
public class TestPasswordValidator : UserNamePasswordValidator
{
public override void Validate(string userName, string password)
{
}
}
private static void StartService()
{
var endpoint = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["HttpsEndpoint"];
var uri = new Uri(endpoint.Protocol + "://" + endpoint.IPEndpoint + "/myservice");
var host = new ServiceHost(typeof(MyService), uri);
host.Credentials.UserNameAuthentication.UserNamePasswordValidationMode = UserNamePasswordValidationMode.Custom;
host.Credentials.UserNameAuthentication.CustomUserNamePasswordValidator = new TestPasswordValidator();
var mexBehavior = new ServiceMetadataBehavior();
mexBehavior.HttpsGetEnabled = true;
mexBehavior.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
host.Description.Behaviors.Add(mexBehavior);
var soapBinding = new WSHttpBinding(SecurityMode.TransportWithMessageCredential);
soapBinding.Security.Message.ClientCredentialType = MessageCredentialType.UserName;
host.AddServiceEndpoint(ServiceMetadataBehavior.MexContractName, MetadataExchangeBindings.CreateMexHttpsBinding(), "mex");
host.AddServiceEndpoint(typeof(IMyService), soapBinding, "Soap");
var restBinding = new WebHttpBinding(WebHttpSecurityMode.Transport);
restBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
var restEndpoint = host.AddServiceEndpoint(typeof(IMyService), restBinding, "");
restEndpoint.Behaviors.Add(new WebHttpBehavior { HelpEnabled = true, DefaultOutgoingResponseFormat = WebMessageFormat.Json, AutomaticFormatSelectionEnabled = true, DefaultBodyStyle = WebMessageBodyStyle.WrappedRequest });
host.Open();
}
public override void Run()
{
StartService();
while (true)
{
Thread.Sleep(10000);
}
}
public override bool OnStart()
{
// Set the maximum number of concurrent connections
ServicePointManager.DefaultConnectionLimit = 12;
// For information on handling configuration changes
// see the MSDN topic at http://go.microsoft.com/fwlink/?LinkId=166357.
return base.OnStart();
}
我收到的錯誤是消息:HTTP無法註冊URL http:// +:20000/myservice /。您的進程無權訪問此名稱空間。 如果我添加 restBinding.HostNameComparisonMode = HostNameComparisonMode.Exact; 它運行,但我無法連接到它。 – biffen 2011-03-03 10:57:39