我在VB.Net中有以下代码。它可以根据需要进行编译和工作。
在我努力转向C#时,每当我开始一个新项目时,我都会努力使用C#,但在这种情况下,我显然遗漏了一些东西。
Private m_Service As ServiceHost = Nothing
Public Function Start(ByVal AddressToConnect As String) As Thread
Dim ServerProcess As New Thread(AddressOf WebService)
ServerProcess.Start(AddressToConnect)
Return ServerProcess
End Function
Private Sub WebService(ByVal AddressToListen As String) ' Address should be in the form: "http://localhost:8000/IHello"
' Define where to listen
Dim Address As Uri = New Uri(AddressToListen)
' Define how to exchange messages
Dim Binding As BasicHttpBinding = New BasicHttpBinding()
' When the connection is established, define what object is created
m_Service = New ServiceHost(GetType(Worker), Address)
' Add an endpoint, passing the address, binding and contract
m_Service.AddServiceEndpoint(GetType([Interface].IContract), Binding, Address)
' YOU MUST PERFORM THE FOLLOWING BEFORE YOU ATTEMPT TO OPEN THE CONNECTION
' netsh http add urlacl url=http://+:80/MyUri user=DOMAIN\user
' Begin listening for connections
m_Service.Open()
Console.WriteLine("WCF Service started")
End Sub
Public Sub [Stop]()
m_Service.Close()
m_Service = Nothing
End Sub
从p到其中一个VB到C#转换站点的C#中说同样如下:
private ServiceHost _Service = null;
public Thread Start(string AddressToConnect)
{
Thread ServerProcess = new Thread(new EventHandler(WebService));
ServerProcess.Start(AddressToConnect);
return ServerProcess;
}
private void WebService(string AddressToListen)
{
// Address should be in the form: "http://localhost:8000/IHello"
// Define where to listen
Uri Address = new Uri(AddressToListen);
// Define how to exchange messages
BasicHttpBinding Binding = new BasicHttpBinding();
// When the connection is established, define what object is created
_Service = new ServiceHost(typeof(Worker), Address);
// Add an endpoint, passing the address, binding and contract
_Service.AddServiceEndpoint(typeof(Interface.IContract), Binding, Address);
// YOU MUST PERFORM THE FOLLOWING BEFORE YOU ATTEMPT TO OPEN THE CONNECTION
// netsh http add urlacl url=http://+:80/MyUri user=DOMAIN\user
// Begin listening for connections
_Service.Open();
Console.WriteLine("WCF Service started");
}
public void Stop()
{
_Service.Close();
_Service = null;
}
问题是编译器抱怨: Thread ServerProcess = new Thread(new EventHandler(WebService));
" WebService没有重载匹配委托EventHandler"
我做错了什么? C#代码应该是什么?
道歉应该是一个简单的问题,但我一直都在与代表们争吵。
- 编辑 -
感谢Markus,这就是我现在所拥有的:
public Thread Start(string AddressToConnect)
{
Thread ServerProcess = new Thread(WebService);
ServerProcess.Start(AddressToConnect);
return ServerProcess;
}
WebService方法现在是:
private void WebService(object AddressToListen)
{
// Address should be in the form: "http://localhost:8000/IHello"
// Define where to listen
Uri Address = new Uri((string)AddressToListen);
// Define how to exchange messages
...
答案 0 :(得分:0)
您可以删除AddressOf
运算符。 C#中不需要它:
Thread ServerProcess = new Thread(WebService);
有一点需要注意的是,提供的方法需要匹配ParameterizedThreadStart
的签名。这会将对象作为参数,因此您需要更改WebService方法的签名:
private void WebService(object AddressToListen)
在该方法中,您必须将对象转换为字符串。