Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
182 views
in Technique[技术] by (71.8m points)

c# - POST Object to VB.NET web service via ASP.NET MVC controller

I'm trying to pass a object from .NET MVC to Web Service (VB.NET) suing SOAP. Passing individual fields works but when I try to pass an object, it throws an error

Cannot convert from Project.Models.Table to Project.WebService.Table

REQUIREMENTS To pass an Object from MVC to Web Service.

Below is my Web Service code.

Questions: Do I need to serialize once I get the object.

 <WebMethod()>
    Public Function FormData(ByVal obj As Table)
        Dim sqlconn As New SqlConnection
        Dim sqlcmd As New SqlCommand
        Try
         Dim formSerializer As New XmlSerializer(GetType(Table))
            Using reader As TextReader = New StringReader(obj)
                data = formSerializer.Deserialize(reader)
            End Using

Below is my MVC Controller, I have added the Service Reference.

 public ActionResult Submission(Table data)
        {
            Table obj = new Table();
            FormService.WebServiceSoapClient client = new FormService.WebServiceSoapClient();
            obj = client.FormData(data);
            return obj;
         }

The error is while passing the data object from Controller to Web Service.

The Table model class with all the fields are added in both the projects.

Your help is appreciated.

Thanks.

UPDATE As per the suggestions of @Panagiotis Kanavos, I tried using AutoMapper. So below is the working code. I am able to store values to database. Please do let me know if its correct and secured way. Thanks.

CLIENT SIDE

public JsonResult PostMethod(Table data)
{
FormService.WebServiceSoapClient client = new FormService.WebServiceSoapClient();
var config = new MapperConfiguration(cfg => { cfg.CreateMap<Table, Table_WS>(); });
IMapper iMapper = config.CreateMapper();
var destination = iMapper.Map<Table, Table_WS>(data);
var result = client.FormData(destination);

WEB SERVICE

<WebMethod()>
Public Function FormData(ByVal Obj As Table_WS) As Table_WS

sqlconn.ConnectionString = CONNECTION_STRING
sqlcmd.Connection = sqlconn
sqlconn.Open()
sqlcmd.CommandType = Data.CommandType.StoredProcedure
sqlcmd.CommandText = "SPR_INSERT"
sqlcmd.Parameters.AddWithValue("@NAME", IIf(IsNothing(Obj.NAME), DBNull.Value, Obj.NAME))

Thank you for helping.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

First of all, if you're passing that much data, I think you should POST it, not GET it cause you clearly attempt to send something, not retrieve. Secondly - I don't know VB.NET, but I assume '' stands for comment so your code will always try to deserialize data as XML. You could try passing parameter to inform your web method which serialization method is used. Again - I'm not sure how it's done in VB.NET.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...