|
I answered this, why did you ask it again ?
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
I have a custom object which is is a simple model with only a few strings and booleans as fields.
I would like to pass this to a web service.
Is there anyway to do this?
I currently have the class in both projects but it says
"CustomClass is not of type web_service_name.CustomClass" when I try to pass it in the consuming project.
How can i tell it they are the same class?
|
|
|
|
|
You can only use 1 definition of the class. Both the UI and the WebService needs to reference a dll that has the definition of the class. It needs to also be serializable because it will be converted to xml for the transaction
<serializable()> _
Public Class Hello
public sName as string
.
.
.
End class
|
|
|
|
|
Thanks but that has nothing to do with my problem.
They are both the same definition.
What I am trying to do is to get it to recognise that.
|
|
|
|
|
You said, that you have the class in both projects. Does both mean you have the class defined in each or referenced in each? Defined will not work, it must be referenced in each....
|
|
|
|
|
for further clarification...
it compares the object using the full assembly name
if your ui project is codeproject and your class is hello
codeproject.hello - is the full class name
if your webservice is webproject and your class is hello
webproject.hello - is the full class name
does codeproject.hello = webproject.hello ?
you think it does but the full assembly names do no match so it thinks they are different objects...
|
|
|
|
|
Exactly!!!
How can I get these to be the same?
Surely it shouldn't be too difficult to just send an object with only a couple of string in it!
|
|
|
|
|
that's what I'm talking about with referencing a dll...
create a dll (let's call it A)
put the definition of the class Hello in A
in your exe reference A
use A.Hello everywhere in your code
in you webservice reference A
use A.Hello everywhere in your code
because the full assembly name is A.hello...when you pass A.hello to your webservice it will see that A.Hello = A.Hello and accept the object as being the same...
-
|
|
|
|
|
Thanks for you help so far.
I have compiled the class into a dll and placed it into the bin directory.
Now what do I do with it?
|
|
|
|
|
i'll assume your exe and your webservice are in the same solution
add the dll project to the solution
in your exe project, do an add reference and point to the dll project
- don't move the dll to the bin when you compile the dll will come along automatically
when you compile
in your webservice project, do an add reference and point to the dll project
- don't move the dll to the bin when you compile the dll will come along automatically
when you compile
|
|
|
|
|
Okay I have imported it into both projects so they are both using the dll.
But it still says
"namespace.classname can not be converted to web_service_name.classname"
Same problem as before.
|
|
|
|
|
Did you remove the old class definitions from your code?
|
|
|
|
|
I have removed the old class.
|
|
|
|
|
Removed the reference and added it again and still no luck.
|
|
|
|
|
before you re-addedit, did you first compile the webservice with the new class?
|
|
|
|
|
are you using web reference or going after the webservice dynamically?
if web reference -- try to remove the reference and re-add it...
|
|
|
|
|
I am adding a web reference to it.
I have removed it, recompiled the service and added it again and still has the same problem.
|
|
|
|
|
Well I have managed to achieve what I want to but in a pretty crude way.
I have written a function to serialize a class and one to deserialize it.
Like this -
Public Function Object2XML(ByVal obj As Object) As String<br />
Dim SW As New StringWriter<br />
Dim Ser As New XmlSerializer(obj.GetType())<br />
Try<br />
Ser.Serialize(SW, obj)<br />
Return SW.ToString<br />
Catch ex As Exception<br />
Throw ex<br />
End Try<br />
End Function<br />
<br />
Public Function XML2OBject(ByVal xml As String, ByVal Type As Type) As Object<br />
Dim Obj As New Object<br />
Dim SR As New StringReader(xml)<br />
Dim Serializer As New System.Xml.Serialization.XmlSerializer(Type)<br />
Try<br />
Return Serializer.Deserialize(SR)<br />
Catch ex As Exception<br />
Throw ex<br />
End Try<br />
End Function
But surely there is a better way to do it than this?
|
|
|
|
|
if you want, you can email your solution and I can take a quick look (strip out any unnecessary code)
|
|
|
|
|
Thats a very kind offer but It is as very large project and I can't strip it easily.
Don't think my boss would appreciate me sending you the whole of our site.
Thanks anyway.
Have you managed to pass a custom class to a web service before successfully?
|
|
|
|
|
yes we do it all the time. we do not however use web references (not that I think that's the problem). we go after the webservice dynamically...
|
|
|
|
|
How do you go after the webservice dynamically as you put it?
|
|
|
|
|
let me put a quick sample together...
|
|
|
|
|
example:
include system.web.services in your exe project
make the webservice (if not already) a virtual directory
the line - Me.Url = "http://localhost/cpwebproj/PopulateMyObj.asmx"
allows you to change localhost to any ip/dns name
hope this helps...
********this goes in your dll***********
<Serializable()> _
Public Class MyObj
Public sName As String
Public sAddr As String
Public sCity As String
Public sState As String
Public sZip As String
End Class
********this goes in your exe***********
Public Class Form1
Inherits System.Windows.Forms.Form
Private Sub Form1_Load1(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim oEntity As New CPDllProj.MyObj
Dim oTrans As New Trans
oEntity = oTrans.Request(oEntity)
MsgBox(oEntity.sName)
MsgBox(oEntity.sAddr)
MsgBox(oEntity.sCity)
MsgBox(oEntity.sState)
MsgBox(oEntity.sZip)
End
End Sub
end class
Imports System.Web.Services
Imports System.Web.Services.Protocols
<System.Diagnostics.DebuggerStepThroughAttribute(), _
System.ComponentModel.DesignerCategoryAttribute("code"), _
System.Web.Services.WebServiceBindingAttribute(Name:="CPHandlerSoap", [Namespace]:="CPWebProj/PopulateMyObj")> _
Public Class Trans
Inherits System.Web.Services.Protocols.SoapHttpClientProtocol
<System.Web.Services.Protocols.SoapDocumentMethodAttribute("CPWebProj/PopulateMyObj/Request", _
RequestNamespace:="CPWebProj/PopulateMyObj", _
ResponseNamespace:="CPWebProj/PopulateMyObj", _
Use:=System.Web.Services.Description.SoapBindingUse.Literal, _
ParameterStyle:=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)> _
Public Function Request(ByVal oEntity As CPDllProj.MyObj) As CPDllProj.MyObj
Dim oa() As Object
Me.Url = "http://localhost/cpwebproj/PopulateMyObj.asmx"
Me.Timeout = 60000
oa = Me.Invoke("Request", New Object() {oEntity})
Return CType(oa(0), CPDllProj.MyObj)
End Function
End Class
********this goes in your webservice***********
Imports System.Web.Services
<System.Web.Services.WebService(Namespace:="CPWebProj/PopulateMyObj")> _
Public Class PopulateMyObj
Inherits System.Web.Services.WebService
#Region " Web Services Designer Generated Code "
Public Sub New()
MyBase.New()
'This call is required by the Web Services Designer.
InitializeComponent()
'Add your own initialization code after the InitializeComponent() call
End Sub
'Required by the Web Services Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Web Services Designer
'It can be modified using the Web Services Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
components = New System.ComponentModel.Container
End Sub
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
'CODEGEN: This procedure is required by the Web Services Designer
'Do not modify it using the code editor.
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
#End Region
<WebMethod(), _
System.Xml.Serialization.XmlInclude(GetType(CPDllProj.MyObj))> _
Public Function Request(ByVal oEntity As CPDllProj.MyObj) As CPDllProj.MyObj
oEntity.sName = "me"
oEntity.sAddr = "123 test st"
oEntity.sCity = "testy town"
oEntity.sState = "IL"
oEntity.sZip = "60000"
Return oEntity
End Function
End Class
|
|
|
|
|
Thanks very much for your help.
But i'm afraid you've got a bit beyond me there.
|
|
|
|