开发者

How do I correctly return values from a web service?

开发者 https://www.devze.com 2023-03-09 17:43 出处:网络
I\'m not familiar with the web services. I created a Email Service using web service. My questions is:

I'm not familiar with the web services. I created a Email Service using web service.

My questions is:

How to make a Email Sending with have a MsgBox "Se开发者_高级运维nt" and " sent Failed"?


You need to create a custom response type and return that from your service. The client (who called the service) should read the response and be responsible for popping up a message box stating whether or not the service call was a success.

Here is what the type could look like:

[DataContract]
public class SendMessageResponse
{
    [DataMember]
    public bool Successful { get; set; }

    [DataMember]
    public List<string> Messages { get; set; }

    public SendMessageResponse()
    {
        this.Successful = true;
        this.Messages = new List<string>();
    }

    public void ProcessException( Exception ex )
    {
        this.Messages.Add( ex.Message );
        this.Successful = false;
    }
}

Here is what the service code would look like:

[WebMethod]
public SendMessageResponse SendMessage(...)
{
    var response = new SendMessageResponse();

    try
    {
        // Do your send message stuff here...
        response.Successful = true; // or whatever you want to say
    }
    catch( Exception ex )
    {
        response.ProcessException( ex );
    }

    return response;
}

This way, your service never faults the channel and will always return your response object (even in the case of an exception). You can then use your response object to communicate to the client the status of your service call.

Here is what the client might look like:

public void SendMessage( MessageServiceClient proxy, string mail, string authstr )
{
    MessageServiceClient.SendMessageResponse response = proxy.SendMessage( mail, authstr );
    if( response.Successful )
    {
        MessageBox.Show( "Message Sent" );
    }
    else
    {
        MessageBox.Show( "Message Failed to Send: " + response.Messages.FirstOrDefault() );
    }
}

The client code is a rough example, but it should give you an idea of what I'm talking about.


try to add a

return "Sent"

on your code..


A Web service sits on the Web Server not on the Client, via visual studio you can add a web reference to the web service, this creates a proxy class, that calls the web service from your website, depending on your response from the website maybe a Boolean you can then tell the client to add a message box.

Cheers

0

精彩评论

暂无评论...
验证码 换一张
取 消