简体   繁体   English

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

[英]Using Windows Azure Relay Service Bus With Windows Phone

I set up a service with windows azure that works perfectly with console app 我使用Windows Azure设置了与控制台应用程序完美配合的服务

I wont past all my service code because it's alot but heres the client code: 我不会跳过所有服务代码,因为很多,但是这里是客户端代码:

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;
        }       
    }

Withi this code, everything is working fine, I run the service and then the client and when i send a msg through the client i can see it in the service screen... 使用此代码,一切正常,我运行服务,然后运行客户端,当我通过客户端发送消息时,我可以在服务屏幕中看到它...

I've tried using this code: 我尝试使用此代码:

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); 
   }

But the only thing it does is returns an xml stating the list of services i have What i need is to send a msg that the service will see 但是它唯一要做的是返回一个xml,说明我拥有的服务列表。我需要发送的是要发送给该服务的msg

any help here? 这里有什么帮助吗?

In the console app, your address prefix is sb , and for your phone app it is https . 在控制台应用程序中,您的地址前缀为sb ,而对于电话应用程序,则为https I am guessing you need to change it to sb to make it work. 我猜您需要将其更改为sb才能使其正常工作。

So it should be: 因此应该是:

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

Also, your console app does not use HelloWorld on the URI at all, so I am not sure why you are using it in the phone app. 另外,您的控制台应用程序根本不使用URI上的HelloWorld ,因此我不确定您为什么在电话应用程序中使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM