Mar 272008
 

I was looking for guidance on this topic, and came up with nothing. I’m sure people are doing this, but can’t find any info. For anyone looking like I was, here’s how to do it.


It’s much simpler than I imagined, thanks to WCF. You can programmatically create your endpoint, binding, and channel inside your service. This would require that the address be hard-coded and require a recompile to change the address or binding. As long as your host’s app.config or web.config has a client endpoint specifying the contract, you don’t have to go through all that work. Your service is simply a client of another service, so your code looks just like that of a client of your service. Furthermore, changing the address or binding is as simple as changing config file values.


Service code:




    1 using System;


    2 using System.ServiceModel;


    3 using DataContracts;


    4 namespace ServiceImplementation


    5 {


    6     [ServiceContract]


    7     public interface IEmailService


    8     {


    9         [OperationContract]


   10         void Send(DataContracts.MailMessage msg);


   11     }


   12 


   13     public class EmailService : IEmailService


   14     {


   15         [OperationBehavior]


   16         public void Send(DataContracts.MailMessage msg)


   17         {


   18             // Open client proxy for legacy web service


   19             using (LegacyEmailServiceClient proxy =


   20                 new LegacyEmailServiceClient())


   21             {


   22                 proxy.SendEmail(msg.To,


   23                     msg.CC,


   24                     msg.Bcc,


   25                     msg.Body,


   26                     msg.Attachments);


   27             }


   28         }


   29     }


   30 }


   31 



Host’s app.config:




    1 <?xml version=1.0 encoding=utf-8?>


    2 <configuration>


    3   <system.serviceModel>


    4     <bindings>


    5       <basicHttpBinding>


    6         <binding name=BasicHttpBinding_Common>


    7           <security mode=None/>


    8         </binding>


    9       </basicHttpBinding>


   10       <netTcpBinding>


   11         <binding name=NetTcpBinding_Common>


   12           <security mode=None/>


   13         </binding>


   14       </netTcpBinding>


   15     </bindings>


   16     <client>


   17       <endpoint address=http://www.gotjeep.net/legacy/email.asmx


   18           binding=basicHttpBinding


   19                 bindingConfiguration=BasicHttpBinding_Common


   20           contract=LegacyEmailServiceClient


   21                 name=LegacyEmailServiceClient />


   22     </client>


   23     <services>


   24       <service name=ServiceImplementation.EmailService


   25               behaviorConfiguration=returnFaults>


   26         <host>


   27           <baseAddresses>


   28             <add baseAddress=http://localhost:8080/EmailService />


   29             <add baseAddress=net.tcp://localhost:8088/EmailService />


   30           </baseAddresses>


   31         </host>


   32         <endpoint name=NetTcpBinding_EmailService


   33                   binding=netTcpBinding


   34                   bindingConfiguration=NetTcpBinding_Common


   35                   contract=ServiceImplementation.IEmailService/>


   36         <endpoint name=BasicHttpBinding_EmailService


   37                   binding=basicHttpBinding


   38                   bindingConfiguration=BasicHttpBinding_Common


   39                   contract=ServiceImplementation.IEmailService/>


   40       </service>


   41     </services>


   42     <behaviors>


   43       <serviceBehaviors>


   44         <behavior name=returnFaults >


   45           <serviceMetadata httpGetEnabled=true />


   46         </behavior>


   47       </serviceBehaviors>


   48     </behaviors>


   49   </system.serviceModel>


   50 </configuration>


 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)

Time limit is exhausted. Please reload the CAPTCHA.