I have the following code in VB.Net. It compiles and works as desired.
In my efforts to move to C#, whenever I work on a new project, I endeavour to use C#, however in this case, I'm clearly missing something.
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
The same in C# from one of those VB to C# conversion sites says the following should work:
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;
}
The problem is that the compiler complains about: Thread ServerProcess = new Thread(new EventHandler(WebService));
"No overload for WebService matches delegate EventHandler"
What am I doing wrong? What should the C# code be?
Apologies for what should be an easy question, but I've always struggled with delegates.
--EDIT--
Thanks to Markus, this is what I have now:
public Thread Start(string AddressToConnect)
{
Thread ServerProcess = new Thread(WebService);
ServerProcess.Start(AddressToConnect);
return ServerProcess;
}
And the WebService method is now:
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
...
You can remove the AddressOf
operator. It is not required in C#:
Thread ServerProcess = new Thread(WebService);
One thing to note though is that the method provided needs to match the signature of ParameterizedThreadStart
. This takes an object as parameter so you need to change the signature of your WebService method:
private void WebService(object AddressToListen)
In the method, you have to convert the object to a string.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.