代码仅在调试模式下执行

时间:2014-05-08 07:39:04

标签: c# wcf named-pipes smpp

以下代码段正在向Windows服务中的另一个自托管WCF服务发送PIN,然后进一步调用SMPP服务器。但这只有在我处于调试模式时才会发生。在正常模式下,它不起作用。

$

        ChannelFactory<IServiceWCF> pipeFactory =
            new ChannelFactory<IServiceWCF>(
        new NetNamedPipeBinding(),
        new EndpointAddress("net.pipe://localhost/MyService/PipeForMyService"));

        pipeProxy = pipeFactory.CreateChannel();

        pipeProxy.Connect();
        pipeProxy.SendPin(pin);

ServiceWCF

$

    public class ServiceWCF : IServiceWCF
{
    PINComposer pinComposer = new PINComposer();

            public bool SendPin(string pin)
    {
        Library.WriteErrorLog("Call Hits. Pin is " + pin);

        PINComposer pinComposer = new PINComposer();

        pinComposer.Connect();            

        pinComposer._client.SendSpeedLimit = 0;

        DataCodings coding = (DataCodings)Enum.Parse(typeof(DataCodings), "Default");

        byte srcTon = byte.Parse("0");
        byte srcNpi = byte.Parse("0");
        string srcAdr = "2378";

        byte dstTon = byte.Parse("1");
        byte dstNpi = byte.Parse("1");
        string dstAdr = "03347823422";

        IList<SubmitSmResp> resp = pinComposer._client.Submit(SMS.ForSubmit()
            .From(srcAdr, srcTon, srcNpi)
            .To(dstAdr, dstTon, dstNpi)
            .Coding(coding)
            .Text(pin + " " + "is your pin code")
            .ExpireIn(TimeSpan.FromDays(2))                
            .DeliveryReceipt()
        );

        Library.WriteErrorLog("SMS Sent");

        return true;
    }

    public bool Connect()
    {            
        pinComposer.Connect();

        return true;
    }
}

PINComposer.CS

$

class PINComposer
{
    public readonly SmppClient _client;        
    private readonly MessageComposer _messageComposer = new MessageComposer();

    public PINComposer()
    {            
        _client = new SmppClient();
        _client.Timeout = 60000;
        _client.NeedEnquireLink = true;
        _client.EnquireInterval = 20;

        _client.RaiseEventsInMainThread = true;

        _client.evConnected += new Inetlab.SMPP.Common.ConnectedEventHandler(client_evConnected);
        _client.evDisconnected += new Inetlab.SMPP.Common.DisconnectedEventHandler(client_evDisconnected);
        _client.evBindComplete += new Inetlab.SMPP.Common.BindRespEventHandler(client_evBindComplete);
        _client.evDeliverSm += new Inetlab.SMPP.Common.DeliverSmEventHandler(client_evDeliverSm);
        _client.evEnquireLink += new Inetlab.SMPP.Common.EnquireLinkEventHandler(client_evEnquireLink);
        _client.evGenericNack += new Inetlab.SMPP.Common.GenericNackEventHandler(client_evGenericNack);
        _client.evUnBind += new Inetlab.SMPP.Common.UnBindEventHandler(client_evUnBind);
        _client.evDataSm += new Inetlab.SMPP.Common.DataSmEventHandler(client_evDataSm);
        _client.evSubmitComplete += new Inetlab.SMPP.Common.SubmitSmRespEventHandler(client_evSubmitComplete);
        _client.evQueryComplete += new Inetlab.SMPP.Common.QuerySmRespEventHandler(client_evQueryComplete);

        _messageComposer.evFullMessageReceived += OnFullMessageReceived;
        _messageComposer.evFullMessageTimedout += OnFullMessageTimedout;
    }

    public void Connect()
    {
        if (_client.Status == ConnectionStatus.Closed)
        {
            _client.AddrNpi = Convert.ToByte(0);
            _client.AddrTon = Convert.ToByte(0);

             _client.EnabledSslProtocols = SslProtocols.None;

            _client.ConnectAsync("172.16.53.39", 2775);

        }
    }

    void client_evConnected(object sender, bool bSuccess)
    {
        if (bSuccess)
        {
            _client.BindAsync("radius", "rad12", ConnectionMode.Transceiver);
            Library.WriteErrorLog("SMPP Service has been connected to server...");
        }
    }

    private void client_evEnquireLink(object sender, EnquireLink data)
    {
        Library.WriteErrorLog("Link with server is alive...");            
    }

    private void client_evGenericNack(object sender, GenericNack data)
    {
        Library.WriteErrorLog("GenericNack received with status " + data.Status.ToString());                   
    }

    private void client_evUnBind(object sender, UnBind data)
    {
        Library.WriteErrorLog("UnBind request received");                   
    }

    void client_evSubmitComplete(object sender, SubmitSmResp data)
    {
        Library.WriteErrorLog("SubmitSmResp received." + " Status: " + data.Status + ", Message Id: " + data.MessageId + ", Sequence: " + data.Sequence); 
    }

    void client_evQueryComplete(object sender, QuerySmResp data)
    {
        Library.WriteErrorLog("QuerySmResp received." + " Status: " + data.Status + ", Message Id: " + data.MessageId + ", Sequence: " + data.Sequence + ", Message State: " + data.MessageState);                   
    }
    private void client_evDeliverSm(object sender, DeliverSm data)
    {
        try
        {
            //Check if we received Delivery Receipt
            if (data.MessageType == MessageTypes.SMSCDeliveryReceipt)
            {
                //Get MessageId of delivered message
                string messageId = data.Receipt.MessageId;
                MessageState deliveryStatus = data.Receipt.State;
            }
            else
            {

                // Receive incoming message and try to concatenate all parts
                if (data.Concatenation != null)
                {

                    _messageComposer.AddMessage(data);

                    Library.WriteErrorLog(
                        string.Format(
                            "DeliverSm part received : Sequence : {0} SourceAddr : {1} Concatenation ( {2} )" +
                            " Coding : {3} Text : {4}",
                            data.Sequence, data.SourceAddr, data.Concatenation, data.DataCoding, data.MessageText));


                }
                else
                {

                    Library.WriteErrorLog("DeliverSm received : "
                              + " Sequence : " + data.Sequence
                              + " SourceAddr : " + data.SourceAddr
                              + " Coding : " + data.DataCoding
                              + " MessageText : " + data.MessageText);

                }
            }
        }
        catch (Exception ex)
        {
            data.Response.Status = CommandStatus.ESME_RX_T_APPN;
            Library.WriteErrorLog("Failed to process DeliverSm" + ex);
        }
    }


    private void client_evDataSm(object sender, DataSm data)
    {

        string messageText = data.Client.GetMessageText(data);

        Library.WriteErrorLog("DataSm received :"
            + " Sequence: " + data.Sequence
            + ", SourceAddr: " + data.SourceAddr
            + ", DestAddr: " + data.DestAddr
            + ", Coding: " + data.DataCoding
            + ", Text: " + messageText);
    }

    void client_evDisconnected(object sender)
    {
        Library.WriteErrorLog("SmppClient disconnected");            

    }

    private void OnFullMessageTimedout(object sender, MessageEventHandlerArgs args)
    {
        Library.WriteErrorLog(string.Format("Incomplete message received from {0}", args.Parts[0].SourceAddr));                      
    }

    private void OnFullMessageReceived(object sender, MessageEventHandlerArgs args)
    {
        Library.WriteErrorLog(string.Format("Full message received from {0}: {1}", args.Parts[0].SourceAddr, args.Text));
    }


    void client_evBindComplete(object sender, Inetlab.SMPP.PDU.BindResp data)
    {
        switch (data.Status)
        {
            case CommandStatus.ESME_ROK:
                Library.WriteErrorLog("SmppClient bound");
                Library.WriteErrorLog("Bind result : system is " + data.SystemId + " with status " + data.Status.ToString());
                break;
            default:
                Library.WriteErrorLog("Bad status returned during Bind : " + data.Command.ToString() + " with status " + data.Status.ToString());                    
                break;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您是如何启动各种项目以及以何种顺序启动的?

如果我没记错的话,&#34; 自动托管&#34;由 WCF开发工具提供仅适用于调试,而在发布中,您必须正确部署&#34;&#34;。

我的帮助文档:Using the WCF Development Tools