Prev: dotnet35sp1
Next: WCF Exception: Type 'MyType' with Data Contract Name 'Some Name'is not Expected
From: Charles on 25 Feb 2010 07:37 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
From: sloan on 25 Feb 2010 08:49 Read this: Shared Types vs Shared Contracts http://blogs.msdn.com/sowmy/archive/2006/06/06/all-about-knowntypes.aspx And this: http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/6b70e9f4-52bc-4fa9-a0ff-c0859e041e85?prof=required and send me a paypal payment, because that took weeks of my time to figure that stuff out !! "Charles" <blank(a)nowhere.com> wrote in message news:%23U6nLdhtKHA.2072(a)TK2MSFTNGP02.phx.gbl... >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 > >
From: sloan on 25 Feb 2010 08:59 PS Since I just went through this: [OperationContract] void Register(INotify notifiable); The above is where I would start: From the second url I showed you: [OperationContract] [ServiceKnownType(typeof(EmployeeConcrete))] void AddPerson(IPerson); What the post says is that you cannot put these into the .config file like you can KnownType. So you can either "hard wire" up the Concrete (as seen above with the EmployeeConcrete).......... or use the "types.txt" solution in the second post. ................. Below is how you can wire up KnownType via config. (seen in the first url I gave you). You ~~cannot~~ do the same with ServiceKnownType (unless you use the workaround via the second url I showed you).........<< and this is the magic nugget of information that will save you hours of trial and error and googling. <system.runtime.serialization> <dataContractSerializer> <declaredTypes> <add type="MyCompany.Library.Shape`1, MyAssembly, Version=2.0.0.0, Culture=neutral, PublicKeyToken=XXXXXX, processorArchitecture=MSIL"> <knownType type="MyCompany.Library.Circle`1, MyAssembly, Version=2.0.0.0, Culture=neutral, PublicKeyToken=XXXXXX, processorArchitecture=MSIL"> <parameter index="0"/> </knownType> </add> </declaredTypes> </dataContractSerializer> </system.runtime.serialization> "sloan" <sloan(a)ipass.net> wrote in message news:eXM9sFitKHA.3904(a)TK2MSFTNGP02.phx.gbl... > > Read this: > Shared Types vs Shared Contracts > > http://blogs.msdn.com/sowmy/archive/2006/06/06/all-about-knowntypes.aspx > > > > And this: > > http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/6b70e9f4-52bc-4fa9-a0ff-c0859e041e85?prof=required > > > > and send me a paypal payment, because that took weeks of my time to figure > that stuff out !! > > > > "Charles" <blank(a)nowhere.com> wrote in message > news:%23U6nLdhtKHA.2072(a)TK2MSFTNGP02.phx.gbl... >>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 >> >> > >
From: Charles on 25 Feb 2010 10:05 Hi Sloan Thanks for the reply. I'll look through the posts carefully. From a quick glance, does it mean that I have to somehow declare the Form1 class to the service as a known type, or have I misunderstood? If I'm right, I was hoping to avoid that by having Form1 implement the INotify interface, which the service _does_ know about, because it defines it. Charles "sloan" <sloan(a)ipass.net> wrote in message news:eXM9sFitKHA.3904(a)TK2MSFTNGP02.phx.gbl... > > Read this: > Shared Types vs Shared Contracts > > http://blogs.msdn.com/sowmy/archive/2006/06/06/all-about-knowntypes.aspx > > > > And this: > > http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/6b70e9f4-52bc-4fa9-a0ff-c0859e041e85?prof=required > > > > and send me a paypal payment, because that took weeks of my time to figure > that stuff out !! > > > > "Charles" <blank(a)nowhere.com> wrote in message > news:%23U6nLdhtKHA.2072(a)TK2MSFTNGP02.phx.gbl... >>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 >> >> > >
From: sloan on 25 Feb 2010 10:34 You question (after only taking a "quick glance") is very hard to understand. >> Public Class Form1 >>>> Implements Microsoft.ServiceModel.Samples.INotify I don't like that design at all. If you want your form to be aware of things (being "Notified"), then you need to declare a object at the class level and subscribe to its events........and not wire up the form directly to the interface. What you're doing (on the surface) seems to be mixing and spaghetti'ing things together. All you need to think is "If Form2 needed to be notified, how much duplicate code would I have"....Your answer would be "Alot". Because you're spaghetti'ing together the Form1 and the "getting notified". I would suggest looking at the Observer Design Pattern. http://www.dofactory.com/Patterns/PatternObserver.aspx And get a feel for it. That is less about WCF and more about OO design. Here is a hint (from the dofactory example) IBM ibm = new IBM("IBM", 120.00); ibm.Attach(new Investor("Sorros")); ibm.Attach(new Investor("Berkshire")); class Investor : IInvestor Then you could raise events in the Investor class.............and then have Form1 (or Form2 or Form3) subscribe to those events. I don't know. You'll have to hash it out, I'm just writing out a few thoughts...............take what I say with a grain of salt. The concept is more important than my exact words. "Observer Pattern" over hardwiring up Form1. <<That's the principal I'm trying to convey. ................ You got some digging to do dude............ A little OO and then some WCF. But try some things out before asking "at a glance" questions........."at a glance" questions usually confuse the audience and the originator. Good luck! "Charles" <blank(a)nowhere.com> wrote in message news:OlqsxvitKHA.4220(a)TK2MSFTNGP05.phx.gbl... > Hi Sloan > > Thanks for the reply. I'll look through the posts carefully. From a quick > glance, does it mean that I have to somehow declare the Form1 class to the > service as a known type, or have I misunderstood? If I'm right, I was > hoping to avoid that by having Form1 implement the INotify interface, > which the service _does_ know about, because it defines it. > > Charles > > > "sloan" <sloan(a)ipass.net> wrote in message > news:eXM9sFitKHA.3904(a)TK2MSFTNGP02.phx.gbl... >> >> Read this: >> Shared Types vs Shared Contracts >> >> http://blogs.msdn.com/sowmy/archive/2006/06/06/all-about-knowntypes.aspx >> >> >> >> And this: >> >> http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/6b70e9f4-52bc-4fa9-a0ff-c0859e041e85?prof=required >> >> >> >> and send me a paypal payment, because that took weeks of my time to >> figure that stuff out !! >> >> >> >> "Charles" <blank(a)nowhere.com> wrote in message >> news:%23U6nLdhtKHA.2072(a)TK2MSFTNGP02.phx.gbl... >>>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 >>> >>> >> >>
|
Next
|
Last
Pages: 1 2 Prev: dotnet35sp1 Next: WCF Exception: Type 'MyType' with Data Contract Name 'Some Name'is not Expected |