Task-Based data service in Silverlight - silverlight

I'm trying to write a silverlight app using the await/async 'pattern' (using SL5 and VS11), but I cannot select the 'Generate Task-Based operations' in the service config, is this supposed to be possible (yet)? Can't find much detail on the web (specific to SL)...

I have been able to create manually Task based asynchronous service proxy in Silverlight 5 and Visual Studio 2010.
Works like charm.
I personally prefer to code my contracts and service proxies manually, but if you like to use Generate Service reference you can take a look at this post - WCF + Tasks.

I think it is not supported in Silverlight but you can do that yourself, Maybe a portable class library would be another workaround for this.
public class MyCServiceProxy
{
public static Task<ObservableCollection<MyC>> GetMyCs()
{
var tcs = new TaskCompletionSource<ObservableCollection<MyC>>();
var client = new MyCServiceClient();
client.GetMyCsCompleted += (s,e) => {};
client.GetMyCsAsync();
return tcs.Task;
}
}

Related

WPF Core DependencyInjection Scoped

I have the following projects:
.Net Standard 2.0 Service Lib
Asp.Net Web API Project
WPF Core 3.1 App
The plan was to share the Service layer between the API and the WPF App.
In the Web API I add a scoped UnitOfWork Service that itself uses scoped DbContexts
services.AddScoped<IUnitOfWork, UnitOfWork>();
The Service Layer uses the UnitOfWork for committing several queries across services (which are added scoped was well) in one transaction. This works as expected.
Service1:
public Service1(IService2 service2,IUnitOfWork unitOfWork)
{
...
}
public async DoSomething()
{
_unitOfWork.DBContext.SomeTable.Add(new SomeTableRecord())
_service2.AddSomeDbRecord(saveInstant:false); //adds a record in another table without saving
await _unitOfWork.SaveAllChanges(); //saves both db changes (inserts) in one transaction
}
Service 2:
public Service2(IUnitOfWork unitOfWork)
{
...
}
public async AddSomeDbRecord(bool saveInstant)
{
_unitOfWork.DBContext.SomeTable2.Add(new SomeTable2Record())
if(saveInstant)
await _unitOfWork.SaveAllChanges(); //saves both db changes (inserts) in one transaction
}
Now when adding my WPF Project where I don't have the Scope of a "WebRequest" anymore, what is a good way to define the scope. Ideally i would like the scope to be around the full chain of calls to a Services method.
I believe this would do what I wanted it to do:
using (var serviceScope = Host.Services.CreateScope())
{
var services = serviceScope.ServiceProvider;
var service1 = services.GetRequiredService<Service1>();
service1.DoSomething();
}
...but this is obviously not nice or straightforward to write for every call to the service lib and I cannot use ViewModel Constructor Injection for the services.
A scope per ViewModel Instance would be okay too I guess but I don't know how to do this (I'm using MVVM Pattern with a ViewModelLocator)
I would want to stay with Microsoft.Extensions.DependencyInjection as the DI framework as this is also what I'm using in the ASP.Net Project.

Dapper: Cannot read/map Geometry type field in local SQL Server

I am running ASP.NET Core 2 application.
I have a local instance of SQL Server where I have a table with a column of type Geometry.
When I go to read this table I get the following errors:
Type Udt is not supported on this platform.
Error parsing column 4 (MyLocation)
However this issue only seems to occur in my API project which calls to a custom made Nuget package that handles the CRUD operations.
If I test the same code in the project that does the CRUD it reads and maps my object.
It is not a connection issue in the API for I can successfully read/write other tables that do not have a Geometry field in it.
What could I possible be missing?
Code:
MyController:
public async Task<IActionResult> Get(Guid Id)
{
var rec = await myRepo.Get<MyData>(id);
// then do stuff
}
*myRepo is injected into my controller.
public class MyData
{
public Guid Id {get;set;}
public IGeometry MyLocation {get;set;}
}
myRepo:
public async Task<TEntity> Get<TEntity>(object id)
where TEntity : class
{
_conn.Open();
return await _conn.GetAsync<TEntity>(id);
}
If this is .NET Core, then I suspect you could have significant issues using sqlgeography etc; UDTs essentially aren't yet implemented in .NET Core:
Additionally, the underlying types that you would want to load use native code; the geo/etc types are not, AFAIK, available in .NET Core.
If I'm wrong, I'm more than happy to try to make whatever changes we need to help make this work, but at the time of writing: I don't think this is going to work through any API (it is not specific to Dapper).
You might want to consider using ASP.NET Core on .NET Framework for today? reference .Net framework 4.5.2 from .Net Core 2.0 project
If this data does actually load from ADO.NET in .NET Core, then I'd be happy to fix whatever I've missed.

Application Insights for WPF Application

There is a WPF application written in Visual Studio.
Can I add Application Insights to this WPF app?
I would like to know how many times a button/tile is clicked. Since there are multiple installations
of the same application, I would like to know which button was clicked how many times from which user/installation. Can this be done with Application Insights?
Thanks
Avanti
While not listed as a supported app type this means there isn't default telemetry data collected/sent to application insights nor is there support for adding AI/creating an application insights resource. That being said it is possible to add to your WPF with a few manual steps so that you can track the specific scenarios you mention (like a button/tile click).
-From Visual studio add the "Application Insights API" NuGet to the project (.11 is the latest today).
This will add the Application Insights API reference and create an application insights configuration file to your project.
The applicationinsights.config file needs to be updated with your instrumentation key as follows:
<?xml version="1.0" encoding="utf-8"?>
<ApplicationInsights xmlns="http://schemas.microsoft.com/ApplicationInsights/2013/Settings" schemaVersion="2014-05-30">
<TelemetryChannel>
<DeveloperMode>false</DeveloperMode>
</TelemetryChannel>
<TelemetryModules>
<Add Type="Microsoft.ApplicationInsights.Tracing.DiagnosticsTelemetryModule, Microsoft.ApplicationInsights"/>
</TelemetryModules>
<InstrumentationKey>**your-instrumentation-key-guid**</InstrumentationKey>
</ApplicationInsights>
To create an application insights instrumentation key login to your azure subscription.
https://portal.azure.com
Click + to create an Application Insights resource.
Then choose the properties tile on the application insights blade and copy the Instrumentation key and add it to your applicationinsights.config file.
Now in your WPF app you can use the Application Insights sdk as described here: http://blogs.msdn.com/b/visualstudioalm/archive/2014/10/21/application-insights-sdk-0-11-0-prerelease.aspx
your events will be visible in the diagnostic search blade which can be selected on the application insights blade.
Note: telemetry is batched locally for 1 min before being sent to the service unless > 500 telemetry events are queued at which point they are sent.
https://azure.microsoft.com/en-us/documentation/articles/app-insights-windows-desktop/
An official link from Microsoft on how to add Application Insights to a Windows Forms application. From the link:
In Azure - portal.azure.com
Create an Application Resource. ::New / Developer Services / Application Insights.
Notice the instrumentation key generated, grab a copy and set it aside, we'll need it when we configure your application.
In Your Application
NuGet - Add 'Application Insights API'
Configure your TelemetryClient.
I'm using MvvmCross in a WPF application, on startup I create a single TelemetryClient that I re-use throughout the application.
var telemetryClient = new TelemetryClient();
telemetryClient.InstrumentationKey = "your key here from Azure";
telemetryClient.Context.Session.Id = Guid.NewGuid().ToString();
telemetryClient.Context.User.AccountId = Username;
telemetryClient.Context.Component.Version = Settings.Default.Version;
telemetryClient.TrackEvent("Application Start");
Mvx.RegisterSingleton<TelemetryClient>(telemetryClient);
Record an event/screen/exception, etc
Any time 'something happens' I'll resolve the TelemetryClient and record the event. This is just like any other Application Insights implementation with regards to tracking and recording.
As an example -
//Resolve the telemetry client
readonly TelemetryClient telemetryClient = Mvx.Resolve<TelemetryClient>();
//Record a page View with some extra information
var pageviewTelemetry = new PageViewTelemetry("Observations");
pageviewTelemetry.Properties.Add("Breadcrumb", breadcrumb);
telemetryClient.TrackPageView(pageviewTelemetry);
//Track an event
var eventTelemetry = new EventTelemetry("Observation Saved");
eventTelemetry.Properties.Add("Saved Observation", observation);
telemetryClient.TrackEvent(eventTelemetry);
//Track an exception
try
{
// do work here
}
catch (Exception ex)
{
telemeteryClient.TrackException(ex);
}
Flush on Application Exit
Application Insights for Windows Desktop applications does not automatically gather/send anything. As a developer one needs to force a flush at application exit.
private void PowerButton_OnClick(object sender, RoutedEventArgs e)
{
var tc = Mvx.Resolve<TelemetryClient>();
if (null != tc)
{
tc.Flush(); // only for desktop apps
}
Application.Current.Shutdown();
}
Or setup an RxTimer to flush on a schedule...I decided to flush every 30 minutes:
var observable = Observable.Interval(new TimeSpan(0, 0, 30, 0));
observable.Subscribe(_ => Application.Current.Dispatcher.Invoke(new Action(() =>
{
var tc = Mvx.Resolve<TelemetryClient>();
if (null != tc)
{
tc.Flush(); // only for desktop apps
Console.WriteLine("Flush TC");
}
})));
FYI - As of 0.17.0 of the Application Insights API NuGet Package if you are offline the flush call doesn't hang, but appears to. Online, the call completes immediately, offline there is a solid 5 second pause before the call completes.
Application Insights (AI) for desktop applications is being deprecated in favor of HockeyApp. It's not overly mature yet, but it works (events essentially reach the same place AI events go).
For example, here's how it looks in RoslynPad (a WPF C# Editor):
using Microsoft.HockeyApp;
//In your initialization method:
var hockeyClient = (HockeyClient)HockeyClient.Current;
hockeyClient.Configure(HockeyAppId)
.RegisterCustomDispatcherUnhandledExceptionLogic(OnUnhandledDispatcherException)
.UnregisterDefaultUnobservedTaskExceptionHandler();
var platformHelper = (HockeyPlatformHelperWPF)hockeyClient.PlatformHelper;
platformHelper.AppVersion = _currentVersion.ToString();
hockeyClient.TrackEvent("App Start");
//sometime later:
hockeyClient.TrackEvent("Something happened");
EDIT Looks like the following NuGet package is required in order for this to work properly: https://www.nuget.org/packages/HockeySDK.WPF.TelemetryWorkaround (see https://github.com/bitstadium/HockeySDK-Windows/pull/88).

Consume SOAP Web Service in Silverlight

I'm attempting to consume a SOAP service in a Silverlight 5 application and I'm completely lost. This is my first Silverlight app and only my second time using web services in a .NET application.
In a separate .NET application, the only way I was able to get it to work was by adding the WSDL as a Web Reference; the application would not build when I added it as a Service Reference. In talking to the WSDL provider, I discovered that the WSDL was compiled using the .NET 2.0 framework...hence the need to add it as a Web Reference.
From the research I've done thus far, I see that Silverlight doesn't support adding a Web Reference. So I tried adding it to the hosting ASP.NET application as a Web Reference then started the server.
Back in my Silverlight app, I selected the option to add a Service Reference and pointed to the WSDL file now at http://localhost:55265/Web%20References/THINKWebService/SLWebSvc_734_Upgrade.wsdl. Visual Studio seemed to pick it up just fine and generate the proxies.
Here's where I start to get stuck. If my research is correct, a WCF reference was created and should be used in that manner. I've never used WCF so I did some reading on how to send/receive requests and this is the best code I've come up with, based on examples in the MSDN library (I inserted it into a button click event so I would know exactly when the code was executing):
private void Button1Click(object sender, RoutedEventArgs e)
{
var client = new ThinkSoapClient();
var userLoginData = new user_login_data {login = "foo", password = "bar"};
var customerIdentifier = new customer_identifier {customer_id = 6677070};
// the debugger halts on this next line and
// references the "dsn"...it's the 4th argument
client.CustomerLoginInfoSelectAsync(userLoginData, customerIdentifier, "", "myDSN");
// I'm not sure if this next line is even needed
client.CustomerLoginInfoSelectCompleted += CustomerLoginInfoSelectCallback;
MessageBox.Show(string.Format("CustomerLoginInfoSelectAsync({0},{1})", userLoginData, customerIdentifier));
}
// here's the callback method
static void CustomerLoginInfoSelectCallback(object sender, CustomerLoginInfoSelectCompletedEventArgs e)
{
MessageBox.Show(string.Format("CustomerLoginInfoSelect Result: {0}", e.Result));
}
As I mentioned in the code above, the debugger halts when executing the client.CustomerLoginInfoSelectAsync method. Here's the error message: XmlSerializer attribute System.Xml.Serialization.XmlAttributeAttribute is not valid in dsn. Only XmlElement, XmlArray, XmlArrayItem and XmlAnyElement attributes are supported when IsWrapped is true.
From the research I've done, I think this error is being caused because the the SOAP action element contains an attribute dsn (not sure, though, if I would be getting this error if the sub-elements also had attributes).
I did a find/replace for IsWrapped=true to IsWrapped=false in Reference.cs but I got the same error but the last word was false instead of true.
I'm not sure if I'm making any sense as to what I'm after, so here's what the generated XML should look like in case it helps:
...
<customer_login_info_select_request dsn="myDSN">
<user_login_data>
<login>foo</login>
<password>bar</password>
</user_login_data>
<customer_identifier>
<customer_id>6677070</customer_id>
</customer_identifier>
<login/> <!--corresponds to the empty string in the call to CustomerLoginInfoSelectAsync-->
</customer_login_info_select_request>
...
So at this point, I'm completely lost. Any insights would be greatly appreciated. If there's any other info I can provide, please let me know.
While possible the normal solution would be to assume it is "just another data source" and use the Web reference on your Server side instead to provide data (and to provide insulation against future changes).
Silverlight App <=> Silverlight Web Services <= External/Legacy Web Service
Keep your Silverlight app slim and let the server do any heavy lifting for you.

Attempting to debug WCF Service added to a solution created with a MVVM Light Toolkit template fails

The goal here is to be able to step into the WCF service code, as well as the Silverlight app code.
File new project > MvvmLight(SL4)
Add new project > WCF Service app
Add service ref to new service in SL proj
In Model\DataService.cs replace GetData with the code below
public void GetData(Action<DataItem, Exception> callback)
{
// Use this to connect to the actual data service
//var item = new DataItem("Welcome to MVVM Light");
var client = new ServiceReference1.Service1Client();
client.GetDataCompleted += (s, e) =>
{
var userCallback = e.UserState as Action;
var item = new DataItem(e.Result);
userCallback(item, null);
};
client.GetDataAsync(123, callback);
}
Place a breakpoint in the GetData method of Service1.svc.cs
F5 to start debugging.
You’ll get a dialog saying you can’t debug.
“The Silverlight project you are about to debug uses web services. Calls to the web service will fail unless the Silverlight project is hosted in and launched from the same web project that contains the web services.”
What do I need to change to allow me to debug the WCF service?
It sounds like your Silverlight application and WCF Service application are using two different ASP.Net projects within your solution. To debug them in a single solution they'd need to be in the same ASP.Net website.

Resources