简体   繁体   English

WCF +双工+ Windows窗体应用程序(ButtonClick)故障

[英]WCF+ duplex + Windows Form Application (ButtonClick) Trouble

I have WCF classes and now project to do. 我有WCF类,现在可以做项目。 My teacher gave as to write app with streaming and duplex (I know that is impossible, but I found backdoor from this situation - I'm sending pics under 60KB). 我的老师愿意编写带有流和双工的应用程序(我知道这是不可能的,但是我从这种情况中发现了后门-我发送的图片小于60KB)。

My code worked well so far as I start wrote my GUI in Windows Form Application. 就我开始在Windows Form Application中编写GUI而言,我的代码运行良好。

When I'm testing it via console - everything work well. 通过控制台进行测试时,一切正常。 But, when I want to use buttons in my GUI i have this exception: 但是,当我想在GUI中使用按钮时,会出现以下异常:

An unhandled exception of type 'System.TimeoutException' occurred in mscorlib.dll Additional information: This request operation sent to net.tcp://localhost:7756/Przesylanie did not receive a reply within the configured timeout (00:00:59.9740007). mscorlib.dll中发生了类型为'System.TimeoutException'的未处理异常。其他信息:发送到net.tcp:// localhost:7756 / Przesylanie的此请求操作在配置的超时(00:00:59.9740007)内未收到答复。 The time allotted to this operation may have been a portion of a longer timeout. 分配给该操作的时间可能是较长超时的一部分。 This may be because the service is still processing the operation or because the service was unable to send a reply message. 这可能是因为该服务仍在处理该操作,或者是因为该服务无法发送回复消息。 Please consider increasing the operation timeout (by casting the channel/proxy to IContextChannel and setting the OperationTimeout property) and ensure that the service is able to connect to the client. 请考虑增加操作超时(通过将通道/代理转换为IContextChannel并设置OperationTimeout属性),并确保该服务能够连接到客户端。

Here bunch of code: 这里是一堆代码:

Service + IService (due to limitation of Stack I put it to one file): 服务+ IService(由于堆栈的限制,我将其放入一个文件):

public void WyslijstrumienNaSerwer()
    {
        IPrzesylanieCallback callback = OperationContext.Current.GetCallbackChannel<IPrzesylanieCallback>();
        string sciezka = @"E:\5 semestr\Fras\Se płotek\Lab6\WcfServiceContractZadanie2\Host\bin\Pliki\" + "plik_odebrany.jpg";
        string filePath = Path.Combine(System.Environment.SystemDirectory, sciezka);
        Console.WriteLine("start");
        callback.WyslijStrumien(filePath);
        Console.WriteLine(filePath);
        Console.WriteLine("meta");
    }

namespace WcfServiceContractZadanie2
{
    [ServiceContract(SessionMode = SessionMode.Required, CallbackContract = typeof(IPrzesylanieCallback))]
    public interface IPrzesylanie
    {
        [OperationContract]
        void WyslijstrumienNaSerwer();
    }

    [ServiceContract]
    public interface IPrzesylanieCallback
    {
        [OperationContract(IsOneWay = true)]
        void WyslijStrumien(string filePath);
    }
}

Client + callback + form + References.cs: 客户端+回调+表单+ References.cs:

namespace Aplikacja
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {

            PrzesylanieClient klient = new PrzesylanieClient(new InstanceContext(new PrzesylanieCallback()), "NetTcpBinding_IPrzesylanie");

            klient.WyslijstrumienNaSerwer();

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());

        }
    }
    public class PrzesylanieCallback : Referencja1.IPrzesylanieCallback
    {
        public void WyslijStrumien(string filePath)
        {
            Form1 o1 = new Form1();

            //  Pobieranie obrazka od klienta
            string sciezka = @"E:\5 semestr\Fras\Se płotek\Lab6\WcfServiceContractZadanie2\Aplikacja\bin\Pliki\" + o1.wybrany();
            string filePathZrodla = Path.Combine(System.Environment.SystemDirectory, sciezka);

            //Otwieranie obrazka
            Stream strumien = null;
            try
            {
                FileStream imageFile = File.OpenRead(filePathZrodla);
                strumien = (Stream)imageFile;
            }
            catch(IOException ioe)
            {
                Console.WriteLine("Wyjatek otwierania pliku: {0}", ioe.ToString());
                Console.ReadKey();
                throw ioe;
            }
            //  Zapisywanie obrazka
            o1.SaveFile(strumien, filePath);
        }
    }
}

 private void btnPrzeslijPlikNaSerwer_Click(object sender, EventArgs e)
        {
               PrzesylanieClient klient = new PrzesylanieClient(new InstanceContext(new PrzesylanieCallback()), "NetTcpBinding_IPrzesylanie");
            klient.WyslijstrumienNaSerwer();
        }

public void WyslijstrumienNaSerwer() {
       base.Channel.WyslijstrumienNaSerwer();
}

I wrote methods SaveFile which works correctly. 我写了方法SaveFile可以正常工作。

As you see, I'm testing my code in the begining of Main function in Client and that works well. 如您所见,我正在Client中Main函数的开头测试我的代码,效果很好。

But when I'm using the same code in Forms it does not work. 但是,当我在Forms中使用相同的代码时,它不起作用。 Compiler is returnig me to References.cs and gives me exception I mentioned earlier. 编译器将我返回给References.cs并给了我前面提到的异常。

Waiting for any respone! 等待任何回应!

My answer does not solve your exception issue; 我的回答不能解决您的例外问题; however, it might prevent you from getting a very bad grade. 但是,这可能会阻止您获得非常差的成绩。 Streaming AND duplex are both supported in WCF. WCF均支持流AND双工。

Streaming: https://msdn.microsoft.com/en-us/library/ms733742(v=vs.110).aspx 流式传输: https : //msdn.microsoft.com/zh-cn/library/ms733742(v= vs.110) .aspx

Duplex: https://msdn.microsoft.com/en-us/library/ms731064(v=vs.110).aspx 双工: https : //msdn.microsoft.com/zh-cn/library/ms731064(v= vs.110) .aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM