C
Charles
I am trying to modify a MSDN WCF sample, and am getting this error in my
client app.
"There was an error while trying to serialize parameter
http://Microsoft.ServiceModel.Samples:obj. The InnerException message was
'Type 'Client.Form1' with data contract name
'Form1:http://schemas.datacontract.org/2004/07/Client' is not expected. Add
any types not known statically to the list of known types - for example, by
using the KnownTypeAttribute attribute or by adding them to the list of
known types passed to DataContractSerializer.'. Please see InnerException
for more details."
The WCF project is hosted by a Windows service, and this builds and starts
up successfully.
The sample is the Calculator sample, which works fine except for my
addition. The purpose of the addition is to allow a client to register
itself with the service so that it can receive notifications whilst it is
alive. The service defines the INotify interface, which is implemented by
the client. The client tries to register itself with the service by sending
a reference to itself in the Register call, but this is where I get the
exception.
I have Googled the exception, but none of the answers I have found address
the particular thing I am trying to do, so they don't really make sense in
my context.
I'm not sure how much code to post, but here is the bulk of the Windows
service
<code>
[ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")]
public interface ICalculator
{
[OperationContract]
double Add(double n1, double n2);
[OperationContract]
double Subtract(double n1, double n2);
[OperationContract]
void Register(INotify notifiable);
}
[ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")]
[DataContractFormat()]
public interface INotify
{
void Notify(string s);
}
// Implement the ICalculator service contract in a service class.
public class CalculatorService : ICalculator
{
private INotify m_Notifiable;
// Implement the ICalculator methods.
public double Add(double n1, double n2)
{
double result = n1 + n2;
m_Notifiable.Notify("Done adding");
return result;
}
public double Subtract(double n1, double n2)
{
double result = n1 - n2;
m_Notifiable.Notify("Done subtracting");
return result;
}
public void Register(INotify notifiable)
{
// Save for notifications
m_Notifiable = notifiable;
}
}
</code>
Here is the client:
<code>
Public Class Form1
Implements Microsoft.ServiceModel.Samples.INotify
Private Client As CalculatorClient
Private Sub ConnectButton_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles ConnectButton.Click
' Step 1: Create an endpoint address and an instance of the WCF
Client.
Dim epAddress As New
EndpointAddress("http://localhost:8000/ServiceModelSamples/Service")
Client = New CalculatorClient(New WSHttpBinding(), epAddress)
'Step 2: Call the service operations.
''m_Notifiable = New Notifiable
Client.Register(Me)
ConnectButton.Enabled = False
DisconnectButton.Enabled = True
End Sub
Private Sub DisconnectButton_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles DisconnectButton.Click
' Step 3: Closing the client gracefully closes the connection and
cleans up resources.
Client.Close()
ConnectButton.Enabled = True
DisconnectButton.Enabled = False
End Sub
Public Sub Notify(ByVal s As String) Implements
Microsoft.ServiceModel.Samples.INotify.Notify
Label1.Text = s
End Sub
End Class
</code>
The exception is thrown on the line
Client.Register(Me)
Can anyone suggest a solution?
TIA
Charles
client app.
"There was an error while trying to serialize parameter
http://Microsoft.ServiceModel.Samples:obj. The InnerException message was
'Type 'Client.Form1' with data contract name
'Form1:http://schemas.datacontract.org/2004/07/Client' is not expected. Add
any types not known statically to the list of known types - for example, by
using the KnownTypeAttribute attribute or by adding them to the list of
known types passed to DataContractSerializer.'. Please see InnerException
for more details."
The WCF project is hosted by a Windows service, and this builds and starts
up successfully.
The sample is the Calculator sample, which works fine except for my
addition. The purpose of the addition is to allow a client to register
itself with the service so that it can receive notifications whilst it is
alive. The service defines the INotify interface, which is implemented by
the client. The client tries to register itself with the service by sending
a reference to itself in the Register call, but this is where I get the
exception.
I have Googled the exception, but none of the answers I have found address
the particular thing I am trying to do, so they don't really make sense in
my context.
I'm not sure how much code to post, but here is the bulk of the Windows
service
<code>
[ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")]
public interface ICalculator
{
[OperationContract]
double Add(double n1, double n2);
[OperationContract]
double Subtract(double n1, double n2);
[OperationContract]
void Register(INotify notifiable);
}
[ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")]
[DataContractFormat()]
public interface INotify
{
void Notify(string s);
}
// Implement the ICalculator service contract in a service class.
public class CalculatorService : ICalculator
{
private INotify m_Notifiable;
// Implement the ICalculator methods.
public double Add(double n1, double n2)
{
double result = n1 + n2;
m_Notifiable.Notify("Done adding");
return result;
}
public double Subtract(double n1, double n2)
{
double result = n1 - n2;
m_Notifiable.Notify("Done subtracting");
return result;
}
public void Register(INotify notifiable)
{
// Save for notifications
m_Notifiable = notifiable;
}
}
</code>
Here is the client:
<code>
Public Class Form1
Implements Microsoft.ServiceModel.Samples.INotify
Private Client As CalculatorClient
Private Sub ConnectButton_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles ConnectButton.Click
' Step 1: Create an endpoint address and an instance of the WCF
Client.
Dim epAddress As New
EndpointAddress("http://localhost:8000/ServiceModelSamples/Service")
Client = New CalculatorClient(New WSHttpBinding(), epAddress)
'Step 2: Call the service operations.
''m_Notifiable = New Notifiable
Client.Register(Me)
ConnectButton.Enabled = False
DisconnectButton.Enabled = True
End Sub
Private Sub DisconnectButton_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles DisconnectButton.Click
' Step 3: Closing the client gracefully closes the connection and
cleans up resources.
Client.Close()
ConnectButton.Enabled = True
DisconnectButton.Enabled = False
End Sub
Public Sub Notify(ByVal s As String) Implements
Microsoft.ServiceModel.Samples.INotify.Notify
Label1.Text = s
End Sub
End Class
</code>
The exception is thrown on the line
Client.Register(Me)
Can anyone suggest a solution?
TIA
Charles