2013-03-18 137 views
0

我設置了一個Windows Azure服務與控制檯應用程序使用Windows Azure的中繼服務總線的Windows Phone

我不會過去我所有的服務代碼,因爲它的很多,但繼承人的客戶端代碼工作完美:

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此代碼,一切正常,我跑業務,然後在客戶端,當我通過客戶端發送味精,我可以看到它的服務畫面...

我已經使用這個嘗試代碼:

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說明服務的名單上有 我需要做的是發出一個味精,該服務將看到

任何幫助嗎?

回答

0

在控制檯應用程序中,您的地址前綴是sb,而對於您的手機應用程序,它是https。我猜你需要將其更改爲sb以使其正常工作。

所以它應該是:

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

此外,您的控制檯應用程序不會對URI使用HelloWorld可言,所以我不知道你爲什麼在手機應用程序中使用它。