在Windows Phone中使用Windows Azure中继服务总线

时间:2013-03-18 14:31:09

标签: c# azure windows-phone-8 azure-servicebusrelay

我使用windows azure设置了一个与控制台应用程序完美配合的服务

我不会通过我的所有服务代码,因为它很多但是客户代码:

static void Main(string[] args)
        {
            // Determine the system connectivity mode based on the command line
            // arguments: -http, -tcp or -auto  (defaults to auto)
            ServiceBusEnvironment.SystemConnectivity.Mode = GetConnectivityMode(args);
            string serviceNamespace = "******";
            string issuerName = "*****";
            string issuerSecret = "*******************************************";

            // create the service URI based on the service namespace
            Uri serviceUri = ServiceBusEnvironment.CreateServiceUri("sb", serviceNamespace, "EchoService");

            // create the credentials object for the endpoint
            TransportClientEndpointBehavior sharedSecretServiceBusCredential = new TransportClientEndpointBehavior();
            sharedSecretServiceBusCredential.TokenProvider = TokenProvider.CreateSharedSecretTokenProvider(issuerName, issuerSecret);            

            // create the channel factory loading the configuration
            ChannelFactory<IEchoChannel> channelFactory = new ChannelFactory<IEchoChannel>("RelayEndpoint", new EndpointAddress(serviceUri));

            // apply the Service Bus credentials
            channelFactory.Endpoint.Behaviors.Add(sharedSecretServiceBusCredential);

            // create and open the client channel
            IEchoChannel channel = channelFactory.CreateChannel();
            channel.Open();


            Console.WriteLine("Enter text to echo (or [Enter] to exit):");
            string input = Console.ReadLine();
            while (input != String.Empty)
            {
                try
                {
                    Console.WriteLine("Server echoed: {0}", channel.Echo(input));
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error: " + e.Message);
                }
                input = Console.ReadLine();
            }

            channel.Close();
            channelFactory.Close();
        }

        static ConnectivityMode GetConnectivityMode(string[] args)
        {
            foreach (string arg in args)
            {
                if (arg.Equals("/auto", StringComparison.InvariantCultureIgnoreCase) ||
                     arg.Equals("-auto", StringComparison.InvariantCultureIgnoreCase))
                {
                    return ConnectivityMode.AutoDetect;
                }
                else if (arg.Equals("/tcp", StringComparison.InvariantCultureIgnoreCase) ||
                     arg.Equals("-tcp", StringComparison.InvariantCultureIgnoreCase))
                {
                    return ConnectivityMode.Tcp;
                }
                else if (arg.Equals("/http", StringComparison.InvariantCultureIgnoreCase) ||
                     arg.Equals("-http", StringComparison.InvariantCultureIgnoreCase))
                {
                    return ConnectivityMode.Http;
                }
            }
            return ConnectivityMode.AutoDetect;
        }       
    }

使用此代码,一切正常,我运行服务然后客户端,当我通过客户端发送消息时,我可以在服务屏幕中看到它...

我尝试过使用此代码:

private void webClient_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
    {
        if (token == "")
        {
            token =
             e.Result.Split('&').Single(x => x.StartsWith("wrap_access_token=",
             StringComparison.OrdinalIgnoreCase)).Split('=')[1];
        }
        string decodedToken = HttpUtility.UrlDecode(token); 

    var uriBuilder =
    new UriBuilder("https://locations.servicebus.windows.net/EchoService");

    uriBuilder.Path += 
    string.Format("HelloWorld"); 

    var webClient = new WebClient();


    if (autho == "")
    {
        autho = string.Format("WRAP access_token=\"{0}\"", decodedToken);
    }
    webClient.Headers["Authorization"] = autho;
    webClient.DownloadStringCompleted += 
    (s, args) => ParseAndShowResult(args);
    webClient.DownloadStringAsync(uriBuilder.Uri);
}
   public void ParseAndShowResult(DownloadStringCompletedEventArgs args)
{
    string result = args.Result;

}

   private void PhoneApplicationPage_Loaded_1(object sender, RoutedEventArgs e)
   {
       var webClient = new WebClient();
       string acsUri = "https://locations-sb.accesscontrol.windows.net/WRAPv0.9/";
       string data = string.Format("wrap_name={0}&wrap_password={1}&wrap_scope={2}",
       HttpUtility.UrlEncode("owner"),
       HttpUtility.UrlEncode("qP/TCHlDzIUidlHC4Q3xNgQn84CLmDx/lYFbDimhj/o="),
       HttpUtility.UrlEncode("http://locations.servicebus.windows.net"));
       webClient.Headers["Content-Type"] = "application/x-www-form-urlencoded";
       webClient.UploadStringCompleted += webClient_UploadStringCompleted;
       webClient.UploadStringAsync(new Uri(acsUri), "POST", data); 
   }

但它唯一能做的就是返回一个xml,说明我拥有的服务列表 我需要的是发送服务将看到的消息

这里有什么帮助吗?

1 个答案:

答案 0 :(得分:0)

在控制台应用中,您的地址前缀为sb,而对于您的手机应用,则为https。我猜您需要将其更改为sb才能使其正常工作。

所以它应该是:

new UriBuilder("sb://locations.servicebus.windows.net/EchoService");

此外,您的控制台应用根本不会在URI上使用HelloWorld,因此我不确定您在手机应用中使用它的原因。