WCF with Duplex Communication - c#

Whenever I am using Window Forms It works Fine But it always give error with console applicion.
Error- The socket connection was aborted. This could be caused by an
error processing your message or a receive timeout being exceeded by
the remote host, or an underlying network resource issue. Local socket
timeout was '00:01:00'.
Here is My Code
Contract
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace ClassLibrary1
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IReportService" in both code and config file together.
[ServiceContract(CallbackContract=typeof(IReportServiceCallbak))]
public interface IReportService
{
[OperationContract(IsOneWay=true)]
void ProcessReport();
}
public interface IReportServiceCallbak
{
[OperationContract(IsOneWay=true)]
void Progress(int percentage);
}
}
Service
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Threading;
namespace ClassLibrary1
{
public class ReportService : IReportService
{
public void ProcessReport()
{
for (int i = 0; i < 100; i++)
{
Thread.Sleep(50);
OperationContext.Current.GetCallbackChannel<IReportServiceCallbak>().Progress(i);
}
}
}
}
Client
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Threading.Tasks;
using System.Threading;
namespace DuplexClientsss
{
class Program
{
static void Main(string[] args)
{
new Tests();
} }
class Tests : ReportService.IReportServiceCallback
{
ReportService.ReportServiceClient obj;
public Tests()
{
obj = new ReportService.ReportServiceClient(new InstanceContext(this));
obj.ProcessReport();
}
public void Progress(int percentage)
{
Console.WriteLine(percentage);
}
}
}

new Task
(
()=>
{
obj.ProcessReport();
}
).Start();

Related

C#, selenium webdriver

I want to create a variable webdriver that i can call in all of my tests.
I currently have the following example for display purposes.
Here I define my webdriver I want to use:
using OpenQA.Selenium;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.PhantomJS;
namespace WebAuto
{
public class OpenBrowser
{
private static IWebDriver driver = new ChromeDriver();
public IWebDriver getDriver()
{
return driver;
}
}
}
Now i want to call this webdriver in another Test called Login:
using OpenQA.Selenium;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.PhantomJS;
namespace WebAuto
{
public class Login
{
public static void Login1()
{
//var driver = new ChromeDriver();
OpenBrowser.IWebDriver. getDriver();
driver.Navigate().GoToUrl("www.anywebsite.com");
//driver.Navigate().GoToUrl("www.anywebsite.com");
//driver.WaitForPageToLoad();
var inputtext1 = driver.FindElement(By.Id("lgLogin_txtUserId"));
//inputtext1.Focus();
inputtext1.SendKeys("User");
var inputpassword1 = driver.FindElement(By.Id("lgLogin_txtPassword"));
//inputpassword1.Focus();
inputpassword1.SendKeys("Password");
var inputbutton1 = driver.FindElement(By.Id("btnLoginClient"));
inputbutton1.Click();
//driver.WaitForPageToLoad();
}
}
}
Could someone please explain what I am missing????
The way to call the driver from Login class is
OpenBrowser openBrowser = new OpenBrowser();
IWebDriver driver = openBrowser.getDriver();

C# Ninject: How to inject dependency depending on field?

A class named Round is a level design in a adventure game.Its filed number indicate which level the player is in. Different level will have different figures to guess. The figures are produced by field FigureFactory.
The question is: using Ninject for dependency injection, how can I set the cooresponding FigureFactory to the variable round, according to the field number? For instance, when field number==1, the cooresponding factory is FigureFactory1, when field number==2, the cooresponding factory is FigureFactory2?
using GuessFigure.Model.Factory;
using Ninject;
using Ninject.Modules;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Ninject.Planning.Bindings;
namespace GuessFigure.Model
{
class Round
{
private int number=1;
private FigureFactory figureFactory;
[Inject]
internal void SetFigureFactory(FigureFactory figureFactory)
{
this.figureFactory = figureFactory;
}
public int[] GetCurrentRoundFigures()
{
return figureFactory.Produce(number);
}
}
//this not work, help please
class RoundModule : NinjectModule
{
public override void Load()
{
Bind<FigureFactory>().To<FigureFactoryRound1>().When(request=>request.ParentRequest.Target.Type.GetField("number").Equals(1));
Bind<FigureFactory>().To<FigureFactoryRound2>().When(request => request.Target.Type.GetField("number").Equals(2));
Bind<FigureFactory>().To<FigureFactoryRound3>().When(request => request.Target.Type.GetField("number").Equals(3));
Bind<FigureFactory>().To<FigureFactoryRound4>().When(request => request.Target.Type.GetField("number").Equals(4));
Bind<FigureFactory>().To<FigureFactoryRound5>().When(request => request.Target.Type.GetField("number").Equals(5));
}
}
}
Factory Method Pattern implementation:
using System;
namespace GuessFigure.Model
{
abstract class FigureFactory
{
protected int figureNumber;
public FigureFactory(int figureNumber)
{
this.figureNumber = figureNumber;
}
internal int[] Produce()
{
int[] figureArray = new int[figureNumber];
for (int i = 0; i < figureNumber; i++)
{
figureArray[i] = Algorithm(i + 1);
}
return figureArray;
}
abstract protected int Algorithm(int inputNumber);
}
}
Concrete Factory(there are still some like this);
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GuessFigure.Model
{
class FigureFactoryRound1 : FigureFactory
{
public FigureFactoryRound1(int figureNumber) : base(figureNumber)
{
}
protected override int Algorithm(int inputNumber)
{
return inputNumber;
}
}
}
class FigureFactoryRound3 :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GuessFigure.Model.Factory
{
class FigureFactoryRound3 : FigureFactory
{
public FigureFactoryRound3(int figureNumber) : base(figureNumber)
{
}
protected override int Algorithm(int inputNumber)
{
return (int) Math.Pow( inputNumber,2) ;
}
}
}
class FigureFactoryRound4 :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GuessFigure.Model.Factory
{
class FigureFactoryRound4 : FigureFactory
{
public FigureFactoryRound4(int figureNumber) : base(figureNumber)
{
}
protected override int Algorithm(int inputNumber)
{
return (int)Math.Pow(inputNumber, 3);
}
}
}
Usage:
IKernel kernel = new StandardKernel(new RoundModule());
Round round = new Model.Round();
round.SetFigureFactory(kernel.Get<FigureFactory>());
int[] array=round.GetCurrentRoundFigures();

c# xamarin - Android background service closing when I close application

My Android service closing when I close the main activity.
I tried add to the AndroidManifest.xml: <service android:name="App7.SimpleService" android:stopWithTask="false">
I also tried the following code for starting service:
Intent intent2 = new Intent(ApplicationContext, typeof(SimpleService));
StartService(intent2);
and
StartService(new Intent("App7.SimpleService"));
I tried also the return START_STICKY; and return StartCommandResult.Sticky;, but no effects.
My code:
SimpleService.cs:
using System;
using System.Threading;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Util;
using System.Net;
using System.IO;
using System.Xml;
using System.Xml.Linq;
using Android.Widget;
using Android;
using App7;
using static System.Net.Mime.MediaTypeNames;
using System.Threading.Tasks;
using System.Text;
namespace SimpleService
{
[Service]
[IntentFilter(new String[] { "App7.SimpleService" })]
public class SimpleServiceBinder : Service
{
public static StartCommandResult START_STICKY { get; private set; }
public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
{
/// My service code
return StartCommandResult.Sticky;
}
public override IBinder OnBind(Intent intent)
{
// binder = new SimpleService(this);
return null;
}
public override void OnDestroy()
{
base.OnDestroy();
}
}
}
MainActivity.cs:
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using App7;
using System.Net;
using System.IO;
using System.Collections.Specialized;
using Android.Webkit;
using System.Threading.Tasks;
namespace SimpleService
{
[Activity(Label = "aplikacja1", MainLauncher = true)]
[Service]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
//////////////////////////////////////
this.StartService(new Intent(this, typeof(SimpleServiceBinder)));
/// I tried also////
// Intent intent2 = new Intent(ApplicationContext, typeof(SimpleService));
// StartService(intent2);
// StartService(new Intent("App7.SimpleService"));
}
}
}
Please, help me

Unable to loading data into gridview

I am using WCF service to display data into gridview but it not properly displaying(Columns are displaying out of order)
Following is my seperate class file containing properties to be added to IList object
Code:
Class1.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace ServiceTest
{
public class Class1
{
public int index { get; set; }
public string name { get; set; }
public int id { get; set; }
}
}
This is Service interface
IService1.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Collections;
namespace ServiceTest
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
[ServiceContract]
public interface IService1
{
[OperationContract]
IList<Class1> GetD();
}
}
This is service class that implements IService.cs
Service1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Collections;
namespace ServiceTest
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
// NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
public class Service1 : IService1
{
public IList<Class1> GetD()
{
IList<Class1> lst = new List<Class1>();
for (int i = 0; i < 50; i++)
{
Class1 c = new Class1();
c.index = i;
c.name = "Madhavi " + i;
c.id = i + 1;
lst.Add(c);
}
return lst;
}
}
}
Below is my consumer code having one gridview databound control.
And consumer code
protected void Page_Load(object sender, EventArgs e)
{
//IList i = new ArrayList();
Service1Client s = new Service1Client();
// i = s.GetD();
GridView1.DataSource = s.GetD();
GridView1.DataBind();
}
Sounds as if you have AutoGenerateColumns set to true for the GridView1. You can instead try explicitly specifying the columns in the .aspx file in the order that you would like these columns to appear and bind each column with the appropriate field from the Datasource.
<asp:GridView runat="server" id="GridView1">
<Columns>
</Columns>
</GridView>

How to write an app-launching application with arguments in C#?

How to if I want to write an application that launches Firefox with arguments ?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace Launcher
{
public static class Program
{
public static void Main(string[] args)
{
Process.Start("C:/Program Files/Mozilla Firefox/firefox.exe");//this is ok
Process.Start("C:/Program Files/Mozilla Firefox/firefox.exe -P MyProfile -no-remote");// this doesn't work
}
}
}
You will need to specify the process.StartInfo.Arguments
See this question: Calling an application from ASP.NET MVC
You will need to use the process.StartInfo.Arguments, as shown here:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace Launcher
{
public static class Program
{
public static void Main(string[] args)
{
Process firefox = new Process();
firefox.StartInfo.FileName = #"C:\Program Files\Mozilla Firefox\firefox.exe";
firefox.StartInfo.Arguments = "-P MyProfile -no-remote";
firefox.Start();
}
}
}

Categories

Resources