2 回答

TA貢獻1831條經驗 獲得超9個贊
是的,正如您所說,客戶端需要在服務器自動關閉之前調用服務。您可以使用以下語句來防止服務器自動關閉。
class Program
{
static void Main(string[] args)
{
Uri uri = new Uri("http://localhost:13020");
BasicHttpBinding binding = new BasicHttpBinding();
binding.Security.Mode = BasicHttpSecurityMode.None;
using (ServiceHost sh=new ServiceHost(typeof(MyService),uri))
{
sh.AddServiceEndpoint(typeof(IService), binding, "");
ServiceMetadataBehavior smb;
smb = sh.Description.Behaviors.Find<ServiceMetadataBehavior>();
if (smb==null)
{
smb = new ServiceMetadataBehavior()
{
HttpGetEnabled = true
};
sh.Description.Behaviors.Add(smb);
}
Binding mexbinding = MetadataExchangeBindings.CreateMexHttpBinding();
sh.AddServiceEndpoint(typeof(IMetadataExchange), mexbinding, "mex");
sh.Opened += delegate
{
Console.WriteLine("service is ready...");
};
sh.Closed += delegate
{
Console.WriteLine("Service is closed now...");
};
sh.Open();
Console.ReadLine();
sh.Close();
}
}
}
一個問題仍然存在:如何讓服務器在沒有管理員權限的情況下運行?
您可以為您的應用程序帳戶提供所需的權限。為此,我們為帳戶保留 URL 命名空間,允許相關應用程序注冊該 URL。我們可以使用Netsh命令(這個工具需要管理員權限)。
Netsh http add urlacl url=http://+:8080/ user=domain\user
您應該用您的實際帳戶代替domain\user,并使用實際端口而不是8080,例如管理員。
Netsh http add urlacl url=http://+:8123/ user=administrator
“ + ”是通配符,表示所有網卡請求都可以訪問這個url。
這是有關此工具的官方文檔。
https://docs.microsoft.com/en-us/windows/desktop/Http/netsh-commands-for-http
如果有什么我可以幫忙的,請隨時告訴我。
- 2 回答
- 0 關注
- 167 瀏覽
添加回答
舉報