Keep WCF Service Connected - c#

How can I keep my WCF Service Client Connected with WinForm even if a Faulted State appened ?
Thanks.

Answer myself :)
You might subscribe to InnerChannel Events
svc.InnerChannel.Closed += InnerChannel_Error;
svc.InnerChannel.Closing += InnerChannel_Error;
svc.InnerChannel.Faulted += InnerChannel_Error;
Then Handle Exceptions and Recreate the Service Proxy
private void InnerChannel_Error(object sender, EventArgs e)
{
var svc = _entrepotService as EntrepotServiceProxy;
try
{
if (svc != null)
{
if (svc.State != CommunicationState.Faulted)
{
svc.Close();
}
else
{
svc.Abort();
}
}
}
catch (CommunicationException)
{
if (svc != null) svc.Abort();
}
catch (TimeoutException)
{
if (svc != null) svc.Abort();
}
catch
{
if (svc != null) svc.Abort();
throw;
}
_entrepotService = new EntrepotServiceProxy();
}

As far as I know, a faulted state is usually terminal to a WCF proxy. So no, I don't think so.

Related

Received "" Error when running the below c# code

I get an error when running the following code. When the program comes to the workbook2 != null line of code, it crashes.
Error:
System.Runtime.InteropServices.COMException (0x80010108): The object invoked has disconnected from its clients. (Exception from HRESULT: 0x80010108 (RPC_E_DISCONNECTED))
at Microsoft.Office.Interop.Excel._Workbook.Close(Object SaveChanges, Object Filename, Object RouteWorkbook)
Code:
public void CompleteAuto()
{
if (workbook1 != null)
{
MessageBox.Show("workbook1 is closed");
workbook1.Close(false);
}
if (workbook2 != null)
{
MessageBox.Show("workbook2is closed");
workbook2.Close(false);
}
if (workbook3 != null)
{
MessageBox.Show("workbook3 is closed");
workbook3.Close(false);
}
if (workbook4 != null)
{
MessageBox.Show("workbook4 is closed");
workbook4.Close(false);
}
if (workbook5 != null)
{
MessageBox.Show("workbook5 is closed");
workbook5.Close(false);
}
if (workbook6 != null)
{
MessageBox.Show("workbook6 is closed");
workbook6.Close(false);
}
if (workbook7 != null)
{
MessageBox.Show("workbook7 is closed");
workbook7.Close(false);
}
if (workbook8 != null)
{
MessageBox.Show("workbook8 is closed");
workbook8.Close(false);
}
if (stepByStep == false)
{
MessageBox.Show("Step by Step done!");
loadingForm.Hide();
}
MessageBox.Show("Output successful!");
this.Close();
}
I am new to c# so I don't know if the above code is right or wrong. I just want to multiple if to check if the workbooks had been open. If yes then closed them, if no then the program keep running.
Could anyone help me? Thanks.
Just a guess, as your example is incomplete...
When you close the Workbook, you are not setting it to null.
Add
Workbook1= null;
after Workbook1.Close.
Same for all the other closures.
You need to make a function, which checks if your workbook is opened.
If it is not, it throws. It is not ideal, but it works.
public static IsOpened(Excel.Application currentApp, string workbook)
{
try
{
var item = currentApp.WorkBooks.get_item(workbook);
return true;
}
catch
{
return false;
}
}
From Bob's answer, I successfully solved my question.
Here is my code:
if (workbook1 != null)
{
workbook1.Close(false);
workbook1 = null;
}
And guess what, it works!!!!

Xamarin : iOS Authentication error with Microsoft login

I am getting this error dialog even after logging in successfully using Microsoft login.
Here is the code to authenticate :
#region IMicrosoftLogin implementation
public async System.Threading.Tasks.Task<Xamarin.Auth.Account> LoginAsync()
{
window = UIApplication.SharedApplication.KeyWindow;
viewController = window.RootViewController;
var auth = new OAuth2Authenticator(
"key_here",//for non- prod
//for production
"openid email https://graph.microsoft.com/user.read",
new Uri("https://login.microsoftonline.com/common/oauth2/V2.0/authorize"),
new Uri("https://myapp_redirect_url"),// for non- prod
null
)
{
AllowCancel = true
};
auth.Completed += Microsoft_Auth_Completed;
var tcs1 = new TaskCompletionSource<AuthenticatorCompletedEventArgs>();
d1 = (o, e) =>
{
try
{
if (e.IsAuthenticated)
{
viewController.DismissViewController(true, null);
tcs1.TrySetResult(e);
}
else
{
viewController.DismissViewController(true, null);
}
}
catch (Exception)
{
tcs1.TrySetResult(new AuthenticatorCompletedEventArgs(null));
}
};
try
{
auth.Completed += d1;
if (viewController == null)
{
while (viewController.PresentedViewController != null)
viewController = viewController.PresentedViewController;
viewController.PresentViewController(auth.GetUI(), true, null);
}
else
{
viewController.PresentViewController(auth.GetUI(), true, null);
UserDialogs.Instance.HideLoading();
}
var result = await tcs1.Task;
return result.Account;
}
catch (Exception)
{
return null;
}
finally
{
auth.Completed -= d1;
}
//auth.Error += (object sender, AuthenticatorErrorEventArgs eventArgs) => {
// auth.IsEnabled = false;
//};
}
private void Microsoft_Auth_Completed(object sender, AuthenticatorCompletedEventArgs e)
{ /// Break point here is not getting triggered.
var authenticator = sender as OAuth1Authenticator;
if (authenticator != null)
{
authenticator.Completed -= Microsoft_Auth_Completed;
}
if (e.IsAuthenticated)
{
var a = e.Account;
}
else
{
}
}
Login async called on button click like this :
btnSignIn.Clicked += async (object sender, EventArgs e) =>
{
if (networkConnection != null && networkConnection.CheckNetworkConnection())
{
UserDialogs.Instance.ShowLoading("Loading", null);
var loginresult = await MicrosoftLogin.LoginAsync();
.....
MicrosoftLogin.cs
namespace projectnamescpace
{
public interface IMicrosoftLogin
{
Task<Account> LoginAsync();
}
}
Please help me.
I have already saw following link solutions and they aren't working for me.
https://forums.xamarin.com/discussion/5866/xamarin-auth-and-infinite-error-alerts
Authentication Error e.Message = OAuth Error = Permissions+error
https://forums.xamarin.com/discussion/95176/forms-oauth-error-after-authenticated-unable-to-add-window-token-android-os-binderproxy
The issue might be caused by the HostName been blocked because of Area Policy .
You could solve this by modifying the DNS (to 8.8.8.8 as an example) for your Mac as well.
Your device, Settings/Wi-Fi
Choose connected Wi-Fi pot
Press DHCP/DNS
Set to 8.8.8.8
Or you could connect phone to the VPN for your apps deployed to device to see corporate servers.

UI suppression - client stuck at SigningIn state

I try to sign in to Skype For Business via my application.
When I'm with UI on, I can sign in.
When I set UI Suppression, my client is stuck at the signing in the state. Neither credentials event nor SigninCallback event nor SignInDelayed event is fired.
Can you help me?
Here is my code:
public void StartUpSkype()
{
try
{
_LyncClient = LyncClient.GetClient();
if (_LyncClient == null)
{
throw new Exception("Unable to obtain client interface");
}
if (_LyncClient.InSuppressedMode == true)
{
if (_LyncClient.State == ClientState.Uninitialized)
{
Object[] _asyncState = { _LyncClient };
_LyncClient.BeginInitialize(InitializeCallback, _asyncState);
}
}
_LyncClient.SignInDelayed += _LyncClient_SignInDelayed;
_LyncClient.StateChanged += _Client_ClientStateChanged;
_LyncClient.CredentialRequested += _LyncClient_CredentialRequested;
}
catch (NotStartedByUserException h)
{
DisplayErrorMessage("Lync is not running");
}
catch (Exception ex)
{
DisplayErrorMessage("General Exception");
}
}
void _Client_ClientStateChanged(Object source, ClientStateChangedEventArgs data)
{
if (data != null)
{
if (data.NewState == ClientState.SignedIn)
{
DisplayErrorMessage("Signed in");
UserIsSignedIn?.Invoke(_LyncClient);
}
if (data.NewState == ClientState.SignedOut)
{
string login = ConfigurationManager.AppSettings["loginSkypeClient"];
string password = ConfigurationManager.AppSettings["pwdSkypeClient"];
_LyncClient.SignInConfiguration.ForgetMe(login);
try
{
// starts the sign in process asynchronously
IAsyncResult asyncResult = _LyncClient.BeginSignIn(login, login, password, SigninCallback, _LyncClient);
// But wait for the results because the events cannot be registered within a worker thread.
asyncResult.AsyncWaitHandle.WaitOne();
}
catch (Exception e)
{
throw e;
}
}
}
}
void _LyncClient_CredentialRequested(object sender, CredentialRequestedEventArgs e)
{
string login = ConfigurationManager.AppSettings["loginSkypeClient"];
string password = ConfigurationManager.AppSettings["pwdSkypeClient"];
if (e.Type == CredentialRequestedType.SignIn)
{
e.Submit(login, password, false);
}
}
private void SigninCallback(IAsyncResult ar)
{
if (ar.IsCompleted == true)
{
try
{
((LyncClient)ar.AsyncState).EndSignIn(ar);
}
catch (RequestCanceledException re)
{
throw re;
}
}
}
private void InitializeCallback(IAsyncResult ar)
{
if (ar.IsCompleted == true)
{
object[] asyncState = (object[])ar.AsyncState;
((LyncClient)asyncState[0]).EndInitialize(ar);
//_ThisInitializedLync is part of application state and is
//a class Boolean field that is set to true if this process
//initialized Lync.
_ThisInitializedLync = true;
}
}
The login + password are correct because I can sign in with them when the UI is on.
The problem only happens when the UI is suppressed.
I've tried many things, I am desperate right now.
Please check the version of SkypeForBusiness. If it is 2016 then it is happening due to ModernAuthentication enabled on both client and tenant office365.
In case ModernAuthentication is enabled, the LyncSDK is not capable enough to handle ModernAuth as SDK doesn't support and Microsoft has no plans to upgrade the SDK.

After scenario Cleanup in Specflow - try catch finally

I am try to clean up the scenario by capturing screenshots
The problem I face is when I try to see how many times "try catch finally" is executed..it's executed nearly 20 times after initial try-finally-it goes directly to catch
Here is my code
[AfterScenario]
public void CleanUp()
{
int aaa = 0;
try
{
if (ScenarioContext.Current.TestError != null)
{
//Taking screenshot
}
Console.WriteLine("try");
}
catch
{
Console.WriteLine("Catch");
if (Browser != null)
{
Browser.Dispose();
Browser = null;
}
return;
}
finally
{
Console.WriteLine("finally");
if (Browser != null)
{
Browser.Dispose();
Browser = null;
Console.WriteLine("finallya");
}
}
return;
}
try
finally
after this why it goes to Catch again? when I am doing some thing wrong here?

Asynchronous WCF service timing out

We have an asynchronous WCF service operation that gets log files from all of the different components of our system and sends them to the client. Since this could take a while if one of the components isn't working right, it would be nice if this functionality won't time out, but it shouldn't cause the client to hang either.
My understanding of asynchronous WCF services is that when the client asks the server for something, the server immediately responds with a message saying, "I'm on it. Keep on doing your own stuff, and I'll let you know when I'm finished." Then the connection is freed up for the client to make other requests while the server spins up a new thread to do the bulk of its work. When the server is finished, it sends a message back to the client with the results. Because of this, the connection between the server and client is free, and regardless of how long the server takes, the connection should never time out. Is this correct?
If that's the case, then our service isn't working as expected. When I test the service, it works as expected as long as it takes less than a minute. If I force it to take longer than that, though, the client throws a TimeoutException. Since the service is asynchronous, shouldn't it never time out? If so, what am I missing?
We wrote our asynchronous service using this page as a guide:
http://code.msdn.microsoft.com/windowsdesktop/How-to-Implement-a-WCF-2090bec8
Here is my code. This is the service contract:
[ServiceContract(CallbackContract = typeof(IInformationServiceCallBack), SessionMode = SessionMode.Required)]
public interface IInformationService
{
//snip...
[OperationContract(AsyncPattern=true)]
[FaultContract(typeof(LogFileFault))]
IAsyncResult BeginGetLogFiles(LogFileRequest[] logfileRequests,
AsyncCallback callback, object state);
LogFile[] EndGetLogFiles(IAsyncResult result);
//snip...
}
This is the service implementation:
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContextMode = InstanceContextMode.PerSession, UseSynchronizationContext=false)]
public class InformationServiceImpl : IInformationService, IDisposable
{
//snip...
public IAsyncResult BeginGetLogFiles(LogFileRequest[] logfileRequests,
AsyncCallback callback, object state)
{
var task = Task<LogFile[]>.Factory.StartNew((x) =>
{
return GetLogFilesHelper(logfileRequests);
}, state);
return task.ContinueWith(res => callback(task));
}
public LogFile[] EndGetLogFiles(IAsyncResult result)
{
var castResult = result as Task<LogFile[]>;
return castResult.Result;
}
private LogFile[] GetLogFilesHelper(LogFileRequest[] logfileRequests)
{
//Long-running method that gets the log files
}
//snip...
}
Here is the client-side code:
public class InformationServiceConnection : WcfDurableConnection<IInformationService> //WcfDurableConnection is one of our internal classes
{
//snip...
public void GetServiceLogFiles(Action<LogFile[], WcfCommandResult> callback)
{
var logfileRequests = new LogFileRequest[]
{
new LogFileRequest(/* snip */),
new LogFileRequest(/* snip */),
new LogFileRequest(/* snip */),
new LogFileRequest(/* snip */)
};
ExecuteTask(x =>
{
LogFile[] logfile = null;
WcfCommandResult wcfResult = null;
var asyncCallback = new AsyncCallback((result) =>
{
logfile = Channel.EndGetLogFiles(result);
callback(logfile, wcfResult);
});
wcfResult = RunCommand(y =>
{
Channel.BeginGetLogFiles(logfileRequests, asyncCallback, null);
}, x);
});
}
/* ExecuteTask and RunCommand are both methods that take care of
* multithreading issues for us. I included their code below in
* case they make a difference, but the code I'm most interested
* in is the GetServiceLogFiles method above. */
//snip...
protected CancellationTokenSource ExecuteTask(Action<CancellationToken> action)
{
CancellationTokenSource tokenSource = new CancellationTokenSource();
ManualResetEvent lastTask;
ManualResetEvent thisTask;
lock (_objectLock)
{
lastTask = _syncTask;
thisTask = new ManualResetEvent(false);
_syncTask = thisTask;
}
tokenSource.Token.Register(x => ((ManualResetEvent)x).Set(), thisTask);
var task = Task.Factory.StartNew((x) =>
{
try
{
lastTask.WaitOne();
action((CancellationToken)x);
}
catch (Exception e)
{
LogUtility.Error(e);
}
finally
{
thisTask.Set();
}
}, tokenSource.Token, tokenSource.Token).HandleExceptions();
return tokenSource;
}
//snip...
protected WcfCommandResult RunCommand(Action<CancellationToken> action, CancellationToken token, bool isRestarting = false)
{
return RunCommand(x => { action(x); return true; }, token, isRestarting);
}
protected WcfCommandResult RunCommand(Func<CancellationToken, bool> action, CancellationToken token, bool isRestarting = false)
{
WcfCommandResult result = new WcfCommandResult();
lock (_reconnectionLock)
{
if (_reconnecting && !isRestarting)
{
result.Completed = false;
return result;
}
}
lock (_channelLock)
{
if (Channel == null && !_closing)
{
token.ThrowIfCancellationRequested();
Channel = GetNewChannel();
var iChannel = (IClientChannel)Channel;
var initResult = Initialize(token, false);
if (initResult.Completed)
{
Connected = true;
LogUtility.Info(string.Format("Connected to {0} at {1}", ServiceName, iChannel.RemoteAddress));
}
else
LogUtility.Info(string.Format("Failed to connect to {0} at {1}", ServiceName, iChannel.RemoteAddress));
}
}
try
{
var channel = Channel;
token.ThrowIfCancellationRequested();
if (channel != null)
result.Completed = action(token);
}
catch (FaultException e)
{
result.Exception = e;
result.Detail = e.GetDetail<DurableFault>();
LogUtility.Error(result.Exception);
}
catch (CommunicationException e)
{
Connected = false;
result.Exception = e;
IClientChannel channel = ((IClientChannel)Channel);
if (channel != null)
channel.Abort();
Channel = null;
if (!_reconnecting)
LogUtility.Error(result.Exception);
}
catch (TimeoutException e)
{
Connected = false;
result.Exception = e;
IClientChannel channel = ((IClientChannel)Channel);
if (channel != null)
channel.Abort();
Channel = null;
if (!_reconnecting)
LogUtility.Error(result.Exception);
}
catch (NullReferenceException e)
{
Connected = false;
result.Exception = e;
IClientChannel channel = ((IClientChannel)Channel);
if (channel != null)
channel.Abort();
Channel = null;
if (!_reconnecting)
LogUtility.WriteException("Channel is null, it has either been disposed or not setup, call BeginSetupUser to create a new channel", e);
}
catch (ObjectDisposedException e)
{
Connected = false;
result.Exception = e;
IClientChannel channel = ((IClientChannel)Channel);
if (channel != null)
channel.Abort();
Channel = null;
if (!_reconnecting)
LogUtility.Error(result.Exception);
}
catch (InvalidOperationException e)
{
Connected = false;
result.Exception = e;
IClientChannel channel = ((IClientChannel)Channel);
if (channel != null)
channel.Abort();
Channel = null;
if (!_reconnecting)
LogUtility.Error(result.Exception);
}
return result;
}
//snip...
}
There is a timeout set in your config file even for asynchronous calls. You should probably increase it if it will take a long time to respond. I think default is 1 minute. In visual studio, go to tools-> WCF Service Configuration Editor to easily change the value.
This may also help you if you want to see what the configuration looks like: Increasing the timeout value in a WCF service
You can set it in that config file, or in the code behind.

Categories

Resources