wcftransport.cs

来自「微软的行业应用解决方案示例」· CS 代码 · 共 64 行

CS
64
字号
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;

using Microsoft.ServiceModel.Channels.Mail;
using Microsoft.ServiceModel.Channels.Mail.WindowsMobile;

namespace StoreAndFwdTransport
{
    public class WcfTransport
    {
        WindowsMobileMailBinding _binding;
        IChannelFactory<IOutputChannel> _outputChannelFactory = null;

        public WcfTransport()
        {
            _binding = new WindowsMobileMailBinding();
            _binding.AcceptExistingMessages = true;
        }

        // net.mail://channelName#foo@bar.com
        public IOutputChannel CreateOutputChannel(string channelName, string emailAddress)
        {
            //create output channel factory if one does not already exist
            if (_outputChannelFactory == null)
            {
                BindingParameterCollection emptyParameterCollection = new BindingParameterCollection();
                _outputChannelFactory = _binding.BuildChannelFactory<IOutputChannel>(emptyParameterCollection);
                _outputChannelFactory.Open();
            }

            //create the output channel
            Uri destinationUri = MailUriHelper.CreateUri(channelName, emailAddress);
            EndpointAddress destinationEndpoint = new EndpointAddress(destinationUri);
            IOutputChannel outputChannel = _outputChannelFactory.CreateChannel(destinationEndpoint);

            outputChannel.Open();

            return outputChannel;
        }

        public IInputChannel AcceptInputChannel(string channelName)
        {
            BindingParameterCollection emptyBindParmCollection = new BindingParameterCollection();
            Uri listenerUri = MailUriHelper.CreateUri(channelName, string.Empty);
            IChannelListener<IInputChannel> listener =
                _binding.BuildChannelListener<IInputChannel>(listenerUri, emptyBindParmCollection);
            listener.Open();

            IInputChannel inputChannel = listener.AcceptChannel();
            inputChannel.Open();

            listener.Close();

            return inputChannel;

        }

    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?