Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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):

C#
// 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);
}


C#
public partial class ReturnValues
{

    public ReturnValues()
    {
    }
}

SQL
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;
    }
}

C#
[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);
  }



C#
// 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);
Posted
Updated 16-Aug-12 10:07am
v2
Comments
Kenneth Haugland 16-Aug-12 16:09pm    
I dont get your issue, just override the Recive MAil async ?

1 solution

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
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 16-Aug-12 16:27pm    
My 5.
--SA
Espen Harlinn 16-Aug-12 17:04pm    
Thank you, Sergey :-D
Abhinav S 16-Aug-12 23:22pm    
Exactly. 5.
Espen Harlinn 17-Aug-12 4:10am    
Thank you, Abhinav :-D
StianSandberg 17-Aug-12 3:09am    
5'ed

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900