Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sending notification from WCF in .NET Framework 4.8 is stuck #366

Open
Thordax opened this issue Oct 7, 2023 · 2 comments
Open

Sending notification from WCF in .NET Framework 4.8 is stuck #366

Thordax opened this issue Oct 7, 2023 · 2 comments

Comments

@Thordax
Copy link

Thordax commented Oct 7, 2023

I have a WCF (Windows Communication Foundation) server. Recently, I tried to update the sending of notifications from a basic https request to Firebase Admin SDK.

The code I'm using in console version works perfectly from my machine. On the other hand, the same code used from the WCF on the same machine remains blocked when sending.

Here's the method I use:

public static async Task<string> SendMessage(string deviceId, string message, string IdSem, string UrlResponseSEM,
    string Auth_Message = "", string auth_key = "", bool isAuthMessageDansNotif = true)
{
    string color = "#0072C6";
    bool hasErrors = false;
    List<string> logList = new List<string> { "[=FCM_SendMessage=]" };

    try
    {
        logList.Add($"[deviceId:{deviceId}] [message:{message}] [IdSem:{IdSem}] [Auth_Message:{Auth_Message}] [color:{color}]");
        logList.Add($"[isAuthMessageDansNotif:{isAuthMessageDansNotif}]");

        Message messagingMessage = null;
        if (isAuthMessageDansNotif)
        {
            messagingMessage = new Message
            {
                Token = deviceId,
                Data = new Dictionary<string, string>
                {
                    { "time", DateTime.Now.ToString() },
                    { "idsem", IdSem },
                    { "urlresponse", UrlResponseSEM },
                    { "message", message },
                    { "auth_message", Auth_Message },
                    { "auth_key", auth_key }
                }
            };
        }
        else
        {
            messagingMessage = new Message
            {
                Token = deviceId,
                Data = new Dictionary<string, string>
                {
                    { "time", DateTime.Now.ToString() },
                    { "idsem", IdSem },
                    { "urlresponse", UrlResponseSEM },
                    { "message", message }
                }
            };
        }

        string postData = JsonConvert.SerializeObject(messagingMessage);
        logList.Add($"[postData={postData}]");

        LogSeo.MyLog.Debug("FCM_SENDING_MESSAGE ...");

        var response = await FirebaseMessaging.DefaultInstance.SendAsync(messagingMessage).ConfigureAwait(false);
        LogSeo.MyLog.Debug("FCM_SENDING_MESSAGE OK");

        return response;
    }
    catch (Exception ex)
    {
        hasErrors = true;
        logList.Add($"ERROR > {ex.GetAllExceptions()} {ex.StackTrace}");
        return $"ERROR:{ex.Message}";
    }
    finally
    {
        logList.Add("[=FCM_SendMessage=]");
        string strLog = string.Join(Environment.NewLine, logList);
        if (hasErrors)
        {
            LogSeo.MyLog.Error(strLog);
        }
        else
        {
            LogSeo.MyLog.Debug(strLog);
        }
    }
}

The method remains blocked at the following line:

var response = await FirebaseMessaging.DefaultInstance.SendAsync(messagingMessage).ConfigureAwait(false);

Could you please tell me what could have caused the problem?

Note that I'm using a proxy and that there may also be a firewall on the machine. Are there any specific ports that need to be unblocked?

If so, how can this work from my executable?

Thanks in advance for your help, and have a nice day,

I tried to send a notification through FCM from my WCF.

@google-oss-bot
Copy link

I found a few problems with this issue:

  • I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.
  • This issue does not seem to follow the issue template. Make sure you provide all the required information.

@Thordax
Copy link
Author

Thordax commented Oct 11, 2023

After many investigations, the problem concerned this code:

options.HttpClientFactory = new ProxyAwareHttpClientFactory();
//...
public class ProxyAwareHttpClientFactory : HttpClientFactory
{
    protected override HttpMessageHandler CreateHandler(CreateHttpClientArgs args)
    {
        var httpClientHandler = new HttpClientHandler
        {
            Proxy = new WebProxy(GlobalSettings.ProxyUrl, GlobalSettings.ProxyPort),
            UseProxy = true
        };
        return httpClientHandler;
    }
}

I thought this code would allow Firebase to send messages via the proxy specified in the ProxyAwareHttpClientFactory. However, this is not the case, the proxy is not used. To get around the problem, I specified the proxy in the web.config file, as follows:

In my web.config :

<system.net>  
    <defaultProxy enabled="true" useDefaultCredentials="true">  
      <proxy  
        usesystemdefault="true"  
        proxyaddress="http://myproxy:8080"  
        bypassonlocal="true"  
      />  
    </defaultProxy>  
  </system.net>

This allows FCM to send requests correctly via the proxy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants