代码仅在调试模式下执行

Posted

技术标签:

【中文标题】代码仅在调试模式下执行【英文标题】:Code is Executing in Debug Mode Only 【发布时间】:2014-05-08 07:39:03 【问题描述】:

以下代码 sn-p 将 PIN 发送到 Windows 服务内的另一个自托管 WCF 服务,然后进一步调用 SMPP 服务器。但这只发生在我处于调试模式时。在正常模式下它不起作用。

$

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

        pipeProxy = pipeFactory.CreateChannel();

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

服务WCF

$

    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;
        
    

【问题讨论】:

“它不起作用”是什么意思?你有什么错误吗?有什么消息吗?有输出吗? 我猜他的意思是永远不会调用 SMPP 服务器 @squelos,我尝试在代码中放置日志,发现,在正常模式下,不会从 smpp 客户端提交调用。供参考,请参阅 SendPin() 方法。我已修改代码以添加日志。 【参考方案1】:

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

如果我没记错的话,WCF 开发工具提供的“自动托管”只在debug中有效发布 您必须“正确”部署。

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

【讨论】:

两个项目都已部署。我正在使用 Fiddler 向 WCF 服务发送请求。

以上是关于代码仅在调试模式下执行的主要内容,如果未能解决你的问题,请参考以下文章

Excel VBA数据验证交换仅在调试模式下工作

仅在调试模式下生成的 C4715

Snackbar 问题(仅在调试模式下)

Visual Studio c++ 仅在调试模式下 LNK 1104 错误

向量下标超出范围,错误仅在调试模式下显示

程序仅在调试器外的发布模式下崩溃