my requirement is to get latitude and longitude from gps, it was working find until I updated the NuGet packages and android sdk. Even if location services are on still I can't access the location.
var locator = CrossGeolocator.Current;
if (locator.IsGeolocationEnabled)
{
locator.DesiredAccuracy = 15;
var position = await
locator.GetPositionAsync(TimeSpan.FromMilliseconds(3000));
Latitude = position.Latitude;
Longitude = position.Longitude;
}
Here if statement returns always false.
My AndroidManifest looks like this..
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.ZameeliNew" android:installLocation="auto">
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="28" />
<application android:label="ZameeliNew.Android" android:usesCleartextTraffic="true" android:icon="#drawable/iconBase">
<meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="AIzaSyBExspvDUbXly-g3YNQdUdnONRSEaHoiNw" />
<uses-library android:name="org.apache.http.legacy" android:required="false" />
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
</manifest>
You are probably using outdated repo with xamarin forms > 4.0. This project was moved to Xamarin.Essentials. I strongly recommend to use this one.
Then in your code you can use for example
internal static async Task<Location> GetDeviceLocation()
{
try
{
var request = new GeolocationRequest(GeolocationAccuracy.Medium);
var location = await Geolocation.GetLocationAsync(request);
return location;
}
catch (Exception ex)
{
Helpers.ShowError("could not locate device: " + ex.Message, 6000);
}
return null;
}
Don't forget to initialize component first.
To check device exceptions:
try
{
var location = await Geolocation.GetLastKnownLocationAsync();
if (location != null)
{
Console.WriteLine($"Latitude: {location.Latitude}, Longitude: {location.Longitude}, Altitude: {location.Altitude}");
}
}
catch (FeatureNotSupportedException fnsEx)
{
// Handle not supported on device exception
}
catch (FeatureNotEnabledException fneEx)
{
// Handle not enabled on device exception
}
catch (PermissionException pEx)
{
// Handle permission exception
}
catch (Exception ex)
{
// Unable to get location
}
Related
referring to Unity project that's targeting both Android and IOS.
Unity - 2020.3.22f1 (LTS)
Firebase SDK - 9.0.0
Trying to implement Firebase push notifications (Cloud Messaging) and having issues with allowing the user to opt-out of those notifications.
I've tried removing the messageReceived event listener which didn't work, the client still received a push notification.
This is how I initialize all of my Firebase APIs including messaging -
//firebase
FirebaseApp.CheckAndFixDependenciesAsync().ContinueWithOnMainThread(task => {
//init analytics
FirebaseAnalytics.SetAnalyticsCollectionEnabled(true);
//init auth
Firebase.Auth.FirebaseAuth auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
auth.SignInAnonymouslyAsync().ContinueWithOnMainThread(task => {
if (task.IsCanceled || task.IsFaulted) {
return;
}
//if authentication worked init rtdb
DatabaseReference = FirebaseDatabase.DefaultInstance.RootReference;
});
//if never initialized before init notifications
if (PlayerPrefs.GetInt("NotificationsFirstLaunch") == 0)
{
//ios permission, should only be invoked once
Firebase.Messaging.FirebaseMessaging.RequestPermissionAsync().ContinueWithOnMainThread(task => { });
EnableNotifications();
PlayerPrefs.SetInt("NotificationsFirstLaunch", 1);
PlayerPrefs.Save();
}
});
public static void EnableNotifications()
{
Firebase.Messaging.FirebaseMessaging.TokenReceived += OnTokenReceived;
Firebase.Messaging.FirebaseMessaging.MessageReceived += OnMessageReceived;
PlayerPrefs.SetInt("Notifications", 1);
PlayerPrefs.Save();
}
public static void DisableNotification()
{
Firebase.Messaging.FirebaseMessaging.TokenReceived -= OnTokenReceived;
Firebase.Messaging.FirebaseMessaging.MessageReceived -= OnMessageReceived;
PlayerPrefs.SetInt("Notifications", 0);
PlayerPrefs.Save();
}
public static void OnTokenReceived(object sender, Firebase.Messaging.TokenReceivedEventArgs token) {
Debug.Log("Received Registration Token: " + token.Token);
}
public static void OnMessageReceived(object sender, Firebase.Messaging.MessageReceivedEventArgs e) {
Debug.Log("Received a new message from: " + e.Message.From);
}
My AndroidManifest was adjusted to use Firebase entry point like such -
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" package="com.unity3d.player" android:installLocation="preferExternal" android:versionCode="1" android:versionName="1.0">
<supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:xlargeScreens="true" android:anyDensity="true" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" tools:node="remove" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" tools:node="remove" />
<application android:theme="#android:style/Theme.NoTitleBar.Fullscreen" android:icon="#drawable/app_icon" android:label="#string/app_name" android:debuggable="false">
<activity android:name="com.google.firebase.MessagingUnityPlayerActivity" android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="unityplayer.UnityActivity" android:value="true" />
</activity>
<service android:name="com.google.firebase.messaging.MessageForwardingService"
android:permission="android.permission.BIND_JOB_SERVICE"
android:exported="false" >
</service>
......
</application>
</manifest>
So basically I have a toggle in my game settings that calls EnableNotifications/DisableNotification.
I've tried using Topics with Subscribe/UnSubscribe but that didn't work as well.
Would appreciate guidance, looking for a solution online just resulted in multiple posts of bugs that never got resolved... Thanks
Adding and removing the listeners from the event didn't help as the token is being received upon app launch anyways, removing it afterward had no effect.
The goto approach to achieve such behavior is indeed using topics with Subscribe/Unsubscribe methods.
Problem is that the approach above is bugged upon second app launch, when testing the Unsubscribe isn't being processed although no exceptions are presented.
The solution I found was refreshing the token upon every subscription/unsubscription.
Example -
public static void EnableNotifications()
{
Firebase.Messaging.FirebaseMessaging.TokenReceived += OnTokenReceived;
Firebase.Messaging.FirebaseMessaging.MessageReceived += OnMessageReceived;
//init notifications
Firebase.Messaging.FirebaseMessaging.DeleteTokenAsync().ContinueWithOnMainThread(task =>
{
Debug.Log($"FirebaseManager - Deleted Token");
Firebase.Messaging.FirebaseMessaging.GetTokenAsync().ContinueWithOnMainThread(task =>
{
Debug.Log($"FirebaseManager - Got New Token");
Firebase.Messaging.FirebaseMessaging.SubscribeAsync(DefaultTopic).ContinueWithOnMainThread(task => {
Debug.Log($"FirebaseManager - Subscribed To Topic - {DefaultTopic}");
});
});
});
PlayerPrefs.SetInt("Notifications", 1);
PlayerPrefs.Save();
}
public static void DisableNotification()
{
//init notifications
Firebase.Messaging.FirebaseMessaging.DeleteTokenAsync().ContinueWithOnMainThread(task =>
{
Debug.Log($"FirebaseManager - Deleted Token");
Firebase.Messaging.FirebaseMessaging.GetTokenAsync().ContinueWithOnMainThread(task =>
{
Debug.Log($"FirebaseManager - Got New Token");
Firebase.Messaging.FirebaseMessaging.UnsubscribeAsync(DefaultTopic).ContinueWithOnMainThread(task => {
Debug.Log($"FirebaseManager - UnSubscribed To Topic - {DefaultTopic}");
});
});
});
PlayerPrefs.SetInt("Notifications", 0);
PlayerPrefs.Save();
}
I am developing a small application using Xamarin forms, I would like to implement (MSAL) active directory, I have configured everything and getting token, after that I have checked in postman while I am getting below
{"success": false," message": "Sorry, you are not authorized to access this API. Please make sure you are logged in!","data":null}
I tried sending the web request to server while I am getting HTTP response below
{StatusCode: 401, ReasonPhrase: 'Unauthorized', Version: 1.1, Content: System.Net.Http.HttpConnection+HttpConnectionResponseContent, Headers: { Transfer-Encoding: chunked Server: Kestrel WWW-Authenticate: Bearer error="invalid_token", error_description="The signature is invalid" X-Rate-Limit-Limit: 12h X-Rate-Limit-Remaining: 4999 X-Rate-Limit-Reset: 2021-07-19T14:20:05.8546118Z Date: Mon, 19 Jul 2021 02:20:05 GMT Content-Type: text/plain }}
Here I tried the code below, please help me to resolve this issue
App.xaml.cs
public static IPublicClientApplication PCA = null;
public static string[] Scopes = { "User.Read" };
PCA = PublicClientApplicationBuilder.Create(Constants.ClientID)
.WithRedirectUri($"msauth://{Constants.AppId}/{Constants.SIGNATURE_HASH}")
.WithAuthority($"https://login.microsoftonline.com/{Constants.TenentID}")
.Build();
Viewmodel signin command i called SignInAsync
public async Task SignInAsync()
{
Microsoft.Identity.Client.AuthenticationResult authResult = null;
IEnumerable<IAccount> accounts = await App.PCA.GetAccountsAsync().ConfigureAwait(false);
try
{
IAccount firstAccount = accounts.FirstOrDefault();
authResult = await App.PCA.AcquireTokenSilent(App.Scopes, firstAccount)
.ExecuteAsync();
Session.Instance.TokenValueFromApp = authResult.AccessToken;
}
catch (MsalUiRequiredException)
{
try
{
authResult = await App.PCA.AcquireTokenInteractive(App.Scopes) .WithParentActivityOrWindow(App.ParentWindow)
.ExecuteAsync();
Session.Instance.TokenValueFromApp = authResult.AccessToken;
}
catch (Exception ex2)
{
}
}
}
Mainactivity.cs
App.ParentWindow = this;
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
AuthenticationContinuationHelper.SetAuthenticationContinuationEventArgs(requestCode, resultCode, data);
}
Manifest.xml
<application android:label="#string/app_name" android:icon="#mipmap/icon">
<activity android:name="microsoft.identity.client.BrowserTabActivity" android:configChanges="orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="msal{clientid}" android:host="auth" /> //here i am pasting client id
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="msauth" android:host="{bundleid}" android:path="/{signaturehash}" /> //here i am pasting bundle id and signature hash
</intent-filter>
</activity>
</application>
<queries>
<package android:name="com.azure.authenticator" />
<package android:name="{bundle id}" /> // here i am pasting a bundle id
<package android:name="com.microsoft.windowsintune.companyportal" />
<intent>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="https" />
</intent>
<intent>
<action android:name="android.support.customtabs.action.CustomTabsService" />
</intent>
</queries>
I've inherited a Xamarin Android project that uses push notifications. The project was using the Google Client Messaging package, I am in the process of converting to the Firebase Messaging package. But I've noticed that there is no .register method for FireBaseMessaging. Do I still need to register with the sender id from the firebase console and if so how?
Old Code:
private void registerInBackground()
{
Task.Run(() =>
{
string msg = "";
try
{
if (gcm == null)
{
gcm = GoogleCloudMessaging.GetInstance(this);
}
regid = gcm.Register(Constants.SenderID);
msg = "Device registered, registration ID=" + regid;
Log.Info(TAG, msg);
// can use GCM/HTTP or CCS to send messages to your app.
sendRegistrationIdToBackend(regid);
// Persist the regID - no need to register again.
storeRegistrationId(this, regid);
}
catch (System.Exception ex)
{
msg = "Error :" + ex.Message;
Log.Error(TAG, msg);
}
finally
{
//RunOnUiThread(() => mDisplay.Append(msg + "\n"));
}
return msg;
});
}
When I change to FireBaseMessaging I notice that on the line regid = gcm.Register(Constants.SenderID); there is no register method on Firebase messaging. Any help would be greatly appreciated.
You don't need to call a register method it will automatically register. You just need to follow this steps:
Add google-services.json to the Android project and make sure to set the build action of that file as GoogleServicesJson.
Add the following in AndroidManifest.xml under application tag:
<receiver android:name="com.google.firebase.iid.FirebaseInstanceIdInternalReceiver" android:exported="false" />
<receiver android:name="com.google.firebase.iid.FirebaseInstanceIdReceiver" android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="${applicationId}" />
</intent-filter>
</receiver>
Implement FirebaseInstanceIdService
[Service, IntentFilter(new[] { "com.google.firebase.INSTANCE_ID_EVENT" })]
public class MyFirebaseInstanceIdService : FirebaseInstanceIdService
{
public override void OnTokenRefresh()
{
var token = FirebaseInstanceId.Instance.Token;
//send token to server here.
}
}
I made a WCF restful service for uploading the files to specific directory on the server, and a small ASP.NET web application that can upload the files threw this web service. The problem is that I am constantly getting the error of access denied. I tried to give all the privileges to a folder for testing purposes (Full control for Everyone, Full control for IIS_IUSRS, AppPool of WCF and Web service, etc.), but still the same error shows. Below is the code of Service and Web application:
Main WCF service class
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple,
InstanceContextMode = InstanceContextMode.PerCall,
IgnoreExtensionDataObject = true,
IncludeExceptionDetailInFaults = true)]
public class UploadFile : IUploadFile
{
public ReturnValue ExcelUpload(Stream File)
{
using (FileStream writer = new FileStream(#"C:\Users\Public\Documents", FileMode.Create))
{
int ReadCount = 0;
var buffer = new byte[8192];
while ((ReadCount = File.Read(buffer, 0, buffer.Length)) != 0)
{
writer.Write(buffer, 0, ReadCount);
}
}
return new ReturnValue() { IsSuccessfull = true };
}
public ReturnValue test(int id)
{
return new ReturnValue() { IsSuccessfull = true };
}
}
WCF service contract
[ServiceContract(Name = "ExcelUpload.IUploadFile")]
public interface IUploadFile
{
[OperationContract]
[DataContractFormat]
[WebInvoke(Method = "*",
UriTemplate = "UploadExcel/",
BodyStyle = WebMessageBodyStyle.Bare,
ResponseFormat = WebMessageFormat.Json)]
ReturnValue ExcelUpload(Stream File);
// TODO: Add your service operations here
[OperationContract]
[DataContractFormat]
[WebInvoke(Method = "POST",
UriTemplate = "test/",
BodyStyle = WebMessageBodyStyle.Bare,
ResponseFormat = WebMessageFormat.Json)]
ReturnValue test(int id);
}
// Use a data contract as illustrated in the sample below to add composite types to service operations.
[DataContract]
public class ReturnValue
{
[DataMember]
public bool IsSuccessfull { get; set; }
}
WCF service web.config
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5"/>
</system.web>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true">
<serviceActivations>
<add relativeAddress="~/UploadFile.svc" service="ExcelUpload.UploadFile"/>
</serviceActivations>
</serviceHostingEnvironment>
<bindings>
<webHttpBinding>
<binding name="crossDomain"
maxBufferSize="2147483647"
maxBufferPoolSize="2147483647"
maxReceivedMessageSize="2147483647"
transferMode="Streamed"
sendTimeout="00:05:00"
crossDomainScriptAccessEnabled="true">
<readerQuotas maxDepth="2147483647"
maxStringContentLength="2147483647"
maxArrayLength="2147483647"
maxBytesPerRead="2147483647"
maxNameTableCharCount="2147483647"/>
<security mode="None" />
</binding>
</webHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="defaultServiceBehaviour">
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="true"/>
<serviceThrottling maxConcurrentCalls="100" maxConcurrentInstances="100" maxConcurrentSessions="100"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="defaultEndpointBehaviour">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service name="ExcelUpload.UploadFile" behaviorConfiguration="defaultServiceBehaviour">
<endpoint address="" behaviorConfiguration="defaultEndpointBehaviour" bindingConfiguration="crossDomain" binding="webHttpBinding" contract="ExcelUpload.IUploadFile"></endpoint>
</service>
</services>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
<add binding="basicHttpBinding" scheme="http"/>
</protocolMapping>
</system.serviceModel>
<system.webServer>
<security>
<authentication>
<anonymousAuthentication enabled="true"/>
</authentication>
<requestFiltering allowHighBitCharacters="true">
<verbs allowUnlisted="false">
<add verb="POST" allowed="true"/>
<add verb="GET" allowed="true"/>
</verbs>
</requestFiltering>
</security>
<modules runAllManagedModulesForAllRequests="true"/>
<!--
To browse web app root directory during debugging, set the value below to true.
Set to false before deployment to avoid disclosing web app folder information.
-->
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
Web application
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="UploadTest.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Uploading using WCF REST API</title>
<script type="text/javascript">
function uploadBlobOrFile(blobOrFile) {
var xhr = new XMLHttpRequest();
xhr.open('POST', 'http://intranet-services.tosama.si/CroSalesExcelUpload/UploadFile.svc/UploadExcel/', true);
xhr.setRequestHeader('Content-length', blobOrFile.size);
xhr.onload = function (e) {
progressBar.value = 0;
progressBar.textContent = progressBar.value;
};
// Listen to the upload progress.
var progressBar = document.querySelector('progress');
xhr.upload.onprogress = function (e) {
if (e.lengthComputable) {
progressBar.value = (e.loaded / e.total) * 100;
progressBar.textContent = progressBar.value; // Fallback.
}
};
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
alert(xhr.responseText);
}
};
xhr.send(blobOrFile);
}
</script>
</head>
<body>
<input id="filePicker" type="file" name="Package" accept="image/*"/>
<br />
<progress min="0" max="100" value="0">0% complete</progress>
<br />
<button title="upload"
onclick="if (filePicker.files[0]) uploadBlobOrFile(filePicker.files[0])">
<span>Upload</span>
</button>
</body>
</html>
Very basic thing is, why are you trying to create object for directory?
#"C:\Users\Public\Documents"
try changing to #"C:\Users\Public\Documents\MyFile.txt"
Here you can use any type of file instead of txt.
And don't forget to set limits for your file to be uploaded.
Hope it helps.. !
Can you please advice why I am not able to start my Windows Service.
As soon as I start it stops immediately giving me the error that no job to do.
Here is the code:
namespace BulkEmailWindowsService
{
public class EmailService : ServiceBase
{
public ServiceHost serviceHost = null;
public EmailService()
{
// Name the Windows Service
ServiceName = "WCFWindowsBulkEmailService";
}
public static void Main()
{
ServiceBase.Run(new EmailService()); //-------- Stops right here..
}
// Start the Windows service.
protected override void OnStart(string[] args)
{
if (serviceHost != null)
{
serviceHost.Close();
}
try
{
Console.WriteLine("Testing 1");
System.Diagnostics.Debugger.Break();
serviceHost = new ServiceHost(typeof(TestBulkEmailService.IBulkEmailService));
serviceHost.Open();
Console.WriteLine("Testing 1");
string logBaseDirectory = "C:\\BulkEmailPrototype\\BulkEmailWindowsService\\BulkEmailWindowsService\\Logs\\BulkEmailWindowsService";
int loggingLevel = int.Parse("5");
int maximumLogFileSize = int.Parse("2");
AppLogger.TraceInfo("Initialization(): Reading configuration settings from config file...");
AppLogger.Init(logBaseDirectory, 0, loggingLevel, "WCFBulkEmail.log", maximumLogFileSize);
AppLogger.TraceInfo("Bulk Email Processing Service is starting....");
using (BulkEmailWindowsService.TestBulkEmailService.BulkEmailServiceClient wfc1 = new BulkEmailWindowsService.TestBulkEmailService.BulkEmailServiceClient())
{
try
{
AppLogger.TraceInfo("Database and Email Processing starting....");
BulkEmailDTOList result1 = new BulkEmailDTOList();
result1 = wfc1.GetBulkEmailInfo(1);
AppLogger.TraceInfo("Database and Email Processing done....");
}
catch
{
AppLogger.TraceInfo("Error in processing Database and Email....");
}
}
serviceHost.Close();
serviceHost = null;
}
catch (Exception ex)
{
// Log the exception.
Console.WriteLine("Error in ONStart ");
AppLogger.TraceInfo("Error in OnStart of Bulk Email Processing Service....");
}
}
protected override void OnStop()
{
if (serviceHost != null)
{
serviceHost.Close();
serviceHost = null;
}
}
}
}
Here is my app.config file:
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IBulkEmailService" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:10:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Windows" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost/TestBulkEmailService/TestBulkEmailService.svc/BulkEmailService"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IBulkEmailService"
contract="TestBulkEmailService.IBulkEmailService" name="BasicHttpBinding_IBulkEmailService" />
</client>
</system.serviceModel>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>
Please note that the WCF Service that is hosted on IIS is running fine and I tested it using a Web App Client. Since I need to run this service constantly on its own (to send emails for bunch of rows from db) hence I am trying to put it in a Windows service with Start and stop. If you know of any other method that is simpler and can do the same please let me know.
this is what I have in my installer
namespace BulkEmailWindowsService
{
// Provide the ProjectInstaller class which allows
// the service to be installed by the Installutil.exe tool
[RunInstaller(true)]
public class ProjectInstaller : Installer
{
private ServiceProcessInstaller process;
private ServiceInstaller service;
public ProjectInstaller()
{
process = new ServiceProcessInstaller();
process.Account = ServiceAccount.LocalSystem;
service = new ServiceInstaller();
service.ServiceName = "WCFWindowsBulkEmailService";
Installers.Add(process);
Installers.Add(service);
}
}
}
So is this not right? I am confused where Main will come.
Try this in you Main method.
private static void Main()
{
try
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new WindowsService()
};
ServiceBase.Run(ServicesToRun);
}
catch (Exception ex)
{
Logger.Error(ex.Message)); // if you have a logger?
}
}
Implementation of windows service should look something like this:
public partial class WindowsService : ServiceBase
{
internal static ServiceHost myServiceHost = null;
public WindowsService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
if (myServiceHost != null)
{
myServiceHost.Close();
}
myServiceHost = new ServiceHost(typeof (EmailService));
myServiceHost.Open();
}
protected override void OnStop()
{
if (myServiceHost != null)
{
myServiceHost.Close();
myServiceHost = null;
}
}
}
Good luck!