2012-07-30 111 views
1

我想要做的是創建一個SocketStreamListener並連接到它(在本地主機上)。然後連接到它併發送一條消息。非常簡單的東西,這一切都完成here in an official demo但我想了解它,並在我自己的應用程序中使用這個邏輯。StreamSocketListener訪問被拒絕

的問題

我創建了一個新的Windows地鐵C#應用程序項目,並有這樣的代碼來創建我的MainPage監聽器:

private void Button_Click(object sender, RoutedEventArgs e) 
{ 
    StreamSocketListener listener = new StreamSocketListener(); 
    greetingOutput.Text = "Hello, " + nameInput.Text + "!"; 
} 

,但我得到這個錯誤:

An exception of type 'System.UnauthorizedAccessException' occurred in HelloWorld.exe but was not handled in user code

WinRT information: At least one of either InternetClientServer or PrivateNetworkClientServer capabilities is required to listen for or receive traffic

Additional information: Access is denied.

If there is a handler for this exception, the program may be safely continued.

儘管在官方演示中有相同的代碼。

我錯過了什麼? 我在做什麼錯?

回答

6

您需要配置您的應用程序需要的必要的能力的一個或兩個根據您的需要:

  1. internetClientServer

    Your Internet connection, including incoming unsolicited connections from the Internet – the app can send information to or from your computer through a firewall. You do not need to declare internetClient if this capability is declared.

  2. privateNetworkClientServer

    A home or work network – the app can send information to or from your computer and other computers on the same network.

(從文檔在http://msdn.microsoft.com/en-us/library/windows/apps/br211423.aspx

也看到這篇文章就如何功能的更多信息,工作原理:http://msdn.microsoft.com/en-us/library/windows/apps/hh464936.aspx

Internet and public networks

The internetClient capability provides outbound access to the Internet and public networks through the firewall. Almost all web apps use this capability. The internetClientServer capability provides inbound and outbound access to the Internet and public networks through the firewall.

Home and work networks

The privateNetworkClientServer capability provides inbound and outbound access to home and work networks through the firewall. This capability is typically used for games that communicate across the local area network (LAN), and for apps that share data across a variety of local devices. If your app specifies musicLibrary, picturesLibrary, or videosLibrary, you don't need to use this capability to access the corresponding library in a Home Group.

你需要聲明你的應用程序需要哪些能力(因此可以訪問) 在你的包裝清單。以下是一步一步的指南:http://msdn.microsoft.com/en-us/library/windows/apps/br211477.aspx

您可以在Visual Studio中使用清單設計器來編輯這些功能。 只需找到並打開名爲package.appxmanifest的解決方案中的文件,即可打開Manifest Designer。

App Manifest Designer

選擇功能選項卡和網絡相關的功能,您的應用要求,你應該是好去。

鏈接到有關應用清單設計器中的文件:http://msdn.microsoft.com/en-us/library/windows/apps/br230259(v=vs.110).aspx

關於最後一段

If there is a handler for this exception, the program may be safely continued.

這只不過是說,你可以在try-catch塊使用StreamSocketListener包裝你的代碼。這是一件好事,如果你想在你的應用程序中優雅地處理缺失的功能:

private void Button_Click(object sender, RoutedEventArgs e) 
{ 
    try 
    { 
     StreamSocketListener listener = new StreamSocketListener(); 
     greetingOutput.Text = "Hello, " + nameInput.Text + "!"; 
    } 
    catch(UnauthorizedAccessException exc) 
    { 
     // Act on the missing capability. Log it and/or warn the user. 
    } 
} 
+0

我對.net並不陌生,但添加功能對我來說是新的。謝謝你的回答。 – darren 2012-07-30 16:39:10