如何从C#中的WCF电子邮件服务返回数组 [英] How to return an array from WCF email service in c#

查看:76
本文介绍了如何从C#中的WCF电子邮件服务返回数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到以下问题:构建电子邮件WCF服务,当该服务收到的请求出现问题时,该电子邮件WCF服务应将返回代表错误的值的数组返回给客户端,否则该服务将接受来自客户端的请求,并且发送电子邮件.

我确实构建了必要的方法以在服务内发送电子邮件并返回值的数组,但是当生成供客户端使用的代理类时,我看到有问题的方法仍被定义为void(不返回数组)值),不知道为什么,这是我的代码(下面您将看到我的服务应用程序中的一些代码,然后是生成的代理类中的一些代码,最后您可以看到使用代理cals对服务的调用) :

I have the following issue: Built an email WCF service that is supposed to return to the client an array of values representing errors when the request received by the service is having problems, otherwise the service would accept the request from the client and send the email.

I did build the needed method to send the email within the service and to return an array of values, but when generating the proxy class for the client to use, I see that the method in question is still defined as void (not returning an array of values), not sure why, here is my code (below you will see some code from my service application, then some code from the generated proxy class and at the end you can see a call made to the service using the proxy calss):

// Below is the method in question (SendEmail) taken from the Server.cs class built within the AppServer project of the service:

  public static MessageContract.ReturnValues SendEmail(MessageContract.EmailMessage emailMessage)
        {
            NameValueCollection mailParams = (NameValueCollection)ConfigurationManager.GetSection("Email");

            MessageContract.ReturnValues errorArray = null;

            errorArray = ErrorCheck.checkNow(emailMessage,mailParams);

          .......

            GC.Collect();
         
            return errorArray;

        }

// Here below is the code taken from the Service.svc class which calls the method SendEmail above:

  public class EmailService : IEmailService
    {   
       public ReturnValues SendEmail(MessageContract.EmailMessage emailMessage)
        {
            ReturnValues arrayOferrors = null;

            try
            {
                arrayOferrors = AppServer.Server.SendEmail(emailMessage);
                            }
            catch (IOException e)
            {
                log.Fatal("I/O exception error: " + "exception: " + e.Message + "Inner exception: " + e.InnerException +  " Stack trace: " + e.StackTrace);
            }
            catch (Exception e)
            {
                log.Fatal("error detected " + "exception: " + e.Message + " Stack trace: " + e.StackTrace);
            }
            return arrayOferrors; 
        }
    }

// Here is the Interface within the service:

 public interface IEmailService
    {
        [OperationContract(IsOneWay = false)]
       MessageContract.ReturnValues SendEmail(MessageContract.EmailMessage emailMsg);
    }

// Here is parts of the code taken from the generated proxy based on the code above (this class is generated using svcutil.exe - notice the comment below: " CODEGEN:..."):

public interface IEmailService
{

    // CODEGEN: Generating message contract since the wrapper name (EmailMessage) of message EmailMessage does not match the default value (SendEmail)
    [System.ServiceModel.OperationContractAttribute(Action="Host.IEmailService/IEmailService/SendEmail", ReplyAction="Host.IEmailService/IEmailService/SendEmailResponse" +
        "")]
    ReturnValues SendEmail(EmailMessage request);
}





public partial class ReturnValues
{

    public ReturnValues()
    {
    }
}


public partial class EmailMessage
{

    [System.ServiceModel.MessageHeaderAttribute(Namespace="Host.IEmailService")]
    public DataContracts.Email EmailParamHeaders;

    [System.ServiceModel.MessageBodyMemberAttribute(Namespace="Host.IEmailService", Order=0)]
    public System.IO.Stream EmailAttachment;

    public EmailMessage()
    {
    }

    public EmailMessage(DataContracts.Email EmailParamHeaders, System.IO.Stream EmailAttachment)
    {
        this.EmailParamHeaders = EmailParamHeaders;
        this.EmailAttachment = EmailAttachment;
    }
}


[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
  ReturnValues IEmailService.SendEmail(EmailMessage request)
  {
      return base.Channel.SendEmail(request);
  }

  public void SendEmail(DataContracts.Email EmailParamHeaders, System.IO.Stream EmailAttachment)
  {
      EmailMessage inValue = new EmailMessage();
      inValue.EmailParamHeaders = EmailParamHeaders;
      inValue.EmailAttachment = EmailAttachment;
      ReturnValues retVal = ((IEmailService)(this)).SendEmail(inValue);
  }







// Below is a call made from console app I built to represent a client (sending request to the email service to send an email) - I want to have it return the array of values to check if there were any errors in my request sent back by the service:

proxy.SendEmail(emailMsg.EmailParamHeaders, result.FileByteStream);

推荐答案

正确"的方法是使用FaultContract,这是一篇文章解释了如何做到这一点:
WCF错误处理和错误转换 [
The "correct" way would be to use a FaultContract, here is an article that explains how to do that:
WCF Error Handling and Fault Conversion[^]

Best regards
Espen Harlinn


这篇关于如何从C#中的WCF电子邮件服务返回数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆